简体   繁体   English

如果父 div 具有某些特定类,如何忽略子 DIV

[英]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 ?如何忽略 class child2 child div 的extra类?

<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 . extra类用于翻译divs内的文本,但我不想用类child2翻译child div 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' 类用于翻译 div 中的文本,但我不想用类 'child2' 翻译子 div。我该如何过滤?

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

This selector is saying这个选择器说

  1. Grab all elements with class extra使用 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获取所有没有child2类的直接/嵌套孩子

You can :你可以 :

  1. loop all div with parent class --> $( ".parent" ).each()使用父类循环所有 div --> $( ".parent" ).each()

  2. check if the current class has a class extra --> if($(this).hasClass( "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" )){}如果当前的父类有一个额外的类循环所有子类并检查它是否有一个类的子类: if($(this).hasClass( "child2" )){} $(this).children().each()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一行代码中的相同逻辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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