gpt4 book ai didi

javascript - jQuery 将 div 识别为列表项

转载 作者:行者123 更新时间:2023-11-28 13:31:40 25 4
gpt4 key购买 nike

我知道你不应该将 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');

fiddle

最佳答案

像这样使用nth-of-type(3n):

$("ul.hello li:nth-of-type(3n)").css('color','red');

例如nth-of-type(3n+2) - 3代表周期大小,n是计数器,2是何时开始

演示:http://jsfiddle.net/N565K/2/

关于javascript - jQuery 将 div 识别为列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24316886/

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