简体   繁体   中英

How do I select all of the elements in a list that have children, using jQuery?

I have an arbitrarily deep list, like so:

<ul>
<li></li>
<li>
  <ul>
    <li></li>
    <li>
      <ul>
        <li></li>
        <li></li>
      </ul>
    </li>
  </ul>
</li>

Using jQuery, how can I select every li in the list that is not a leaf node. In other words, I want to select all of the li elements that have children ULs.

Thanks for your help!

jQuery('li:has(ul)');

有关:has更多信息

Here's another alternative:

$('li ul').closest('li')

This is likely to be a fair bit faster than :has on modern browsers, since it'll use the native querySelectorAll method on the main selector. :has can't use native support since it's not a standard CSS selector but a jQuery extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM