gpt4 book ai didi

jquery - 如何将Class添加到其相等索引.....(JQUERY)

转载 作者:行者123 更新时间:2023-12-03 22:56:56 25 4
gpt4 key购买 nike

<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 节点,请使用 .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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com