How do I select the next "n" elements starting from the current element in jQuery?

Hri*_*sto 46 jquery jquery-selectors

How do I select the next "n" elements starting from the current element? What I mean is...

 $(this).attr(...);
Run Code Online (Sandbox Code Playgroud)

I want to do this "n" times. For the example of n=4:

$(this).attr(...);
$(this).next().attr(...);
$(this).next().next().attr(...);
$(this).next().next().next().attr(...);
Run Code Online (Sandbox Code Playgroud)

or perhaps do it in a loop:

for (i = 0; i < n; i++) {
    $(this).next().attr(...);
}
Run Code Online (Sandbox Code Playgroud)

How can I do this? Is there a way I can do this by selecting the next "n" elements or in a loop?

jig*_*fox 61

这应该工作:

$(this).nextAll().slice(0,4).attr(…)
Run Code Online (Sandbox Code Playgroud)

更新:

这也有效:

$(this).nextAll("*:lt(4)").attr(…)
Run Code Online (Sandbox Code Playgroud)

  • @Hristo - 如果你也需要原始的那个,你可以调用`$(this).nextAll().和Self()...`. (8认同)

Dan*_*ett 10

nextAll方法选择元素的以下兄弟节点,可选地由选择器过滤.然后,您可以使用a slice来限制更小的n.