简体   繁体   中英

How to ignore child DIV if parent div has some specific class

How can I ignore the class extra for child div with class child2 ?

<div class="parent extra">
    <div class="child1">
        Some text
    </div>
    <div class="child2">
        Some text
    </div>
</div>

The extra class is used for translating the texts inside divs ,but I don't want to translate the child div with class child2 . How can I filter this?

The 'extra' class is used for translating the texts inside divs,but I don't want to translate the child div with class 'child2'.How can I filter this?

$('.extra :not(.child2)')

This selector is saying

  1. Grab all elements with class extra
  2. Search all direct/nested children(the meaning of the space)
  3. Grab all direct/nested children which do not have a class of child2

You can :

  1. loop all div with parent class --> $( ".parent" ).each()

  2. check if the current class has a class extra --> if($(this).hasClass( "extra" ){}

  3. if the current parent class has a class extra loop all children and check if it has a child with class : child2 $(this).children().each() and if($(this).hasClass( "child2" )){}

  4. If it is the case get the parent and do some process...

See Fiddle

EDIT:

same logic in one line code

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