开发者

jQuery selector: select all `li` elements after `li[id="control"]`

开发者 https://www.devze.com 2023-02-19 13:44 出处:网络
I have a list: <ul> <li class=\"myclass\">...</li> <l开发者_JAVA技巧i class=\"myclass\">...</li>

I have a list:

<ul>
   <li class="myclass">...</li>
   <l开发者_JAVA技巧i class="myclass">...</li>   
   <li class="myclass">...</li>
   <li id="control">...</li>
   <li class="myclass">...</li>
   <li class="myclass">...</li>
   <li class="myclass">...</li>
</ul>

How can I select all li elements after li[id="control"]?

In this example, I need to select last three li's


There's a .nextAll() which will get all siblings after a particular element selection, like this:

$("#control").nextAll();

You can try it out here

In this case the siblings are <li> elements, so it'll select all of them on the same level (not child <li>s beneath, if they exist).


If you want a single selector, I think you can use $('#control ~ li'). The "~" connector (is "connector" the right word? "operator" maybe?) is like "+", but it selects all following siblings and not just the first one.

Sometimes it's nice to be able to do things with a single selector string, because you may want to organize selectors and actions in a data structure. However, it's quite often the case that doing explicit jQuery function calls in a chain (as in @Nick's answer with ".nextAll()") is faster than letting the selection engine parse and evaluate a fancy selector string.

0

精彩评论

暂无评论...
验证码 换一张
取 消