有没有办法缩短多个.next()?

时间:2022-01-12 11:04:41

If you have

如果你有

$('#element').next().next().next().next().next().doSomething();
$('#element').next().next().next().next().doSomething();
$('#element').next().next().next().doSomething();

Essentially, doing something different to sibling elements.

基本上,做一些与兄弟元素不同的东西。

Is there a way to turn loads of next() into a specific one? For instance I wish I could do next(5) = next() five times.

有没有办法将next()的负载转换为特定的?例如,我希望我可以做下一次(5)= next()五次。

  1. Is there a way to do this?
  2. 有没有办法做到这一点?
  3. Is there a way to do this for prev() as well?
  4. 有没有办法为prev()做这个?

2 个解决方案

#1


7  

You can use .nextAll() and :eq()

你可以使用.nextAll()和:eq()

.nextAll(':eq(4)')

.nextAll( ':当量(4)')

and for previous you can use .prevAll()

以前你可以使用.prevAll()

.prevAll(':eq(4)');

#2


3  

Yes!

是!

.nextUntil()

See this for all the details: http://api.jquery.com/nextUntil/

有关所有详细信息,请参阅此处:http://api.jquery.com/nextUntil/

EDIT: Another answerer said .nextAll(':eq(4)')

编辑:另一位回答者说.nextAll(':eq(4)')

This is true except - what if you don't know how many elements? Well, you can find out of course but .nextUntil will work with a fluid and changing list of siblings until it meets a certain selector.

这是真的,除非 - 如果你不知道有多少元素怎么办?好吧,你可以找到当然但是.nextUntil将使用流畅且不断变化的兄弟姐妹列表,直到遇到某个选择器。

#1


7  

You can use .nextAll() and :eq()

你可以使用.nextAll()和:eq()

.nextAll(':eq(4)')

.nextAll( ':当量(4)')

and for previous you can use .prevAll()

以前你可以使用.prevAll()

.prevAll(':eq(4)');

#2


3  

Yes!

是!

.nextUntil()

See this for all the details: http://api.jquery.com/nextUntil/

有关所有详细信息,请参阅此处:http://api.jquery.com/nextUntil/

EDIT: Another answerer said .nextAll(':eq(4)')

编辑:另一位回答者说.nextAll(':eq(4)')

This is true except - what if you don't know how many elements? Well, you can find out of course but .nextUntil will work with a fluid and changing list of siblings until it meets a certain selector.

这是真的,除非 - 如果你不知道有多少元素怎么办?好吧,你可以找到当然但是.nextUntil将使用流畅且不断变化的兄弟姐妹列表,直到遇到某个选择器。