如何在 jQuery 中选择每 n 个特定子元素?
我有这样的标记:
<div id="container">
<h1>Heading</h1>
<p>Some text</p>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<!-- ... same thing on down the page ... -->
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
</div>
我想为每第四个 div 添加一个类。这是我期望可以工作的 jQuery:
$('div.foo:nth-child(4n)').addClass('bar');
但它的结果是:
<div id="container">
<h1>Heading</h1>
<p>Some text</p>
<div class="foo">Some stuff</div>
<div class="foo bar">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo bar">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo bar">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo bar">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
</div> <!-- #container -->
所以,显然所有子元素都被计算在内,并且只有匹配的元素才会添加类。我可以考虑其他两个元素并使用 :nth-child(4n+2)
,但我不能总是指望 div
之前恰好有两个元素代码>s.
是否有一个类似 nth-child 的选择器(或方法),在计数时只考虑指定的元素?
I have this markup:
<div id="container">
<h1>Heading</h1>
<p>Some text</p>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<!-- ... same thing on down the page ... -->
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
</div>
I'd like to add a class to every fourth div. Here's the jQuery that I expected would work:
$('div.foo:nth-child(4n)').addClass('bar');
But it results in:
<div id="container">
<h1>Heading</h1>
<p>Some text</p>
<div class="foo">Some stuff</div>
<div class="foo bar">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo bar">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo bar">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo bar">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
<div class="foo">Some stuff</div>
</div> <!-- #container -->
So, obviously all children are being counted, and only the matched element gets the class added. I could just take those other two elements into consideration and use :nth-child(4n+2)
, but I can't always count on there being exactly two elements preceding my div
s.
Is there an nth-child-like selector (or method) that will only take the specified element into consideration when counting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用过滤器函数来获取每第四个元素,如下所示:
工作示例@:
You can use the filter function to get every 4th element as follows:
Working example @:
在 jQuery 1.9+ 中
:nth-of-type
可以在这里工作。$('div.foo:nth-of-type(4n)').addClass('bar');
参考:对 :nth-of 的一个很好的描述-type 与 :nth-child
In jQuery 1.9+
:nth-of-type
would work here.$('div.foo:nth-of-type(4n)').addClass('bar');
Reference: A good description of :nth-of-type vs :nth-child
你总是可以自己做:
You could always do it yourself:
http://api.jquery.com/filter/#using-filter-function
很棒的教程。关于使用过滤器。
基本思想是使用模数运算符 (%) 与每个或过滤器结合来迭代元素,并且仅在模数没有返回余数时才应用操作(即,迭代的索引是您想要的倍数。)这是很多情况下的常见做法。
您还可以对每个函数执行类似的操作。
http://api.jquery.com/filter/#using-filter-function
Great tutorial. On using filter.
The basic idea is to use the modulus operator (%) in combination with each or filter to iterate over the elements and only apply actions when the modulus returns no remainder (ie, the index of the iteration is a multiple that you desire.) This is a common practice in a lot of contexts.
You can also do something similar with the each function.