Efficient, concise way to find next matching sibling?

前端 未结 3 1192
你的背包
你的背包 2020-11-27 18:35

Sticking to the official jQuery API, is there a more concise, but not less efficient, way of finding the next sibling of an element that matches a given selector other than

3条回答
  •  青春惊慌失措
    2020-11-27 19:10

    You can pass a multiple selector to .nextAll() and use .first() on the result, like this:

    var nextFoo = $(this).nextAll("div.foo, div.something, div.else").first();
    

    Edit: Just for comparison, here it is added to the test suite: http://jsperf.com/jquery-next-loop-vs-nextall-first/2 This approach is so much faster because it's a simple combination of handing the .nextAll() selector off to native code when possible (every current browser) and just taking the first of the result set....way faster than any looping you can do purely in JavaScript.

提交回复
热议问题