我知道你不应该将 div 放入无序列表中,但假设我们有下面提到的标记。使用 jQuery,我想定位每个第三个列表项:li:nth-child(3n)。看起来 jQuery 也将里面的 div 算作列表项,即使我们指定 li:eq(3n)。无论父元素中还有什么,我们如何检测每个第三个列表项。当 'li' 被忽略时,甚至编写 li:eq(3n) 有什么意义?
html:
<ul class="hello">
<div class="someDiv">a</div>
<li>one</li>
<li>two</li>
<li>three</li>
<div class="divider">b</div>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
jQuery:
$("ul.hello li:nth-child(3n)").css('color','red');
最佳答案
像这样使用nth-of-type(3n)
:
$("ul.hello li:nth-of-type(3n)").css('color','red');
例如nth-of-type(3n+2) - 3代表周期大小,n是计数器,2是何时开始
关于javascript - jQuery 将 div 识别为列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24316886/