简体   繁体   中英

Jquery: selecting elements that do not only have children with the same class name?

How do I make a jQuery selector that selects all elements with the class name "foo", except for those that ONLY have children with the class name of "bar".

<!-- Don't select this -->
<div class="foo">
    <div class="bar"></div>
    <div class="bar"></div>
    <div class="bar"></div>
    <div class="bar"></div>
    <div class="bar"></div>
</div>

<!-- Don't select this -->
<div class="foo">
    <div class="bar"><p>Some text</p></div>
    <div class="bar"></div>
    <div class="bar"></div>
    <div class="bar"><p>Some text</p></div>
    <div class="bar"></div>
</div>


<!-- Select this -->
<div class="foo">
    <p>Some text</p>
    <div class="bar"></div>
    <div class="bar"></div>
    <div class="bar"></div>
    <div class="bar"></div>
</div>

<!-- Select this -->
<div class="foo">
    <p>Some text</p>
</div>

Something like this?

$('.foo > :not(.bar)').closest('.foo');

Demo

Start from all .foo , find immediate children except those that have the bar class, and revert back to the .foo .

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