<ul>
   <li>Index 0</li>
   <li>Index 1</li>
   <li>Index 2</li>
</ul>
<div>
     <a>Index 0</a>
     <a>Index 1 (If i click this this this i want to addClass to it to LI with the same index of this )</a>
     <a>Index 2</a>
</div>

最佳答案

为此,您可以调用jQuery的.index() help方法。它返回相对于当前节点同级的索引。要查找具有相应索引的li node,请使用.eq() help

$('a').click(function() {
   $('ul li').eq($(this).index()).addClass('your_new_class');
});

演示:http://www.jsfiddle.net/2rFn3/

引用您的评论
$('a').click(function() {
   $('ul li').eq($(this).index()).addClass('your_new_class').siblings().removeClass('your_new_class');
});

演示:http://www.jsfiddle.net/2rFn3/1/

关于jquery - 如何将Class添加到其相等的索引中……(JQUERY),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4791576/

10-12 14:54
查看更多