如何在 jQuery 中选择每 n 个特定子元素?

发布于 2024-10-17 09:59:07 字数 1941 浏览 2 评论 0原文

我有这样的标记:

<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 divs.

Is there an nth-child-like selector (or method) that will only take the specified element into consideration when counting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吃颗糖壮壮胆 2024-10-24 09:59:07

您可以使用过滤器函数来获取每第四个元素,如下所示:

$('div.foo').filter(function(index){
 return (index%4 == 3);
}).addClass('bar');

工作示例@:

http://jsfiddle.net/wCxSv/

You can use the filter function to get every 4th element as follows:

$('div.foo').filter(function(index){
 return (index%4 == 3);
}).addClass('bar');

Working example @:

http://jsfiddle.net/wCxSv/

挥剑断情 2024-10-24 09:59:07

在 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

尤怨 2024-10-24 09:59:07

你总是可以自己做:

$('div.foo').each( function(i) {
  if( i % 4 != 3 )
    return
  $(this).addClass('bar')
})

You could always do it yourself:

$('div.foo').each( function(i) {
  if( i % 4 != 3 )
    return
  $(this).addClass('bar')
})
暖风昔人 2024-10-24 09:59:07

http://api.jquery.com/filter/#using-filter-function
很棒的教程。关于使用过滤器。

基本思想是使用模数运算符 (%) 与每个或过滤器结合来迭代元素,并且仅在模数没有返回余数时才应用操作(即,迭代的索引是您想要的倍数。)这是很多情况下的常见做法。
您还可以对每个函数执行类似的操作。

$('div.foo').each(function(index) {
  if((index+1)%4 == 0){
    $(this).addClass('bar');
 });

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.

$('div.foo').each(function(index) {
  if((index+1)%4 == 0){
    $(this).addClass('bar');
 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文