javascript - 当所有 child 都有特定类(class)时隐藏父 div

标签 javascript jquery html css

我有这样的 html:

<div class="woof_container woof_container_producer">
 <div class="woof_container_inner">
  <div class="woof_block_html_items">
   <ul class="woof_list">
    <li class="woof_term hide">Nike<span class="quantity">(0)</span></li>
    <li class="woof_term hide">Reebok<span class="quantity">(0)</span></li>
    <li class="woof_term hide">Adidas<span class="quantity">(0)</span></li>
    <li class="woof_term hide">Puma<span class="quantity">(0)</span></li>
   </ul>
  </div>
 </div>
</div>

<div class="woof_container woof_container_category">
 <div class="woof_container_inner">
  <div class="woof_block_html_items">
   <ul class="woof_list">
    <li class="woof_term hide">Jacket<span class="quantity">(0)</span></li>
    <li class="woof_term">T-shirt<span class="quantity">(3)</span></li>
    <li class="woof_term hide">Dress<span class="quantity">(0)</span></li>
    <li class="woof_term hide">Boots<span class="quantity">(0)</span></li>
   </ul>
  </div>
 </div>
</div>

我需要的是隐藏整个 ".woof_container",当里面的所有 "li" 元素都有 ".hide" 类时。

所以在这个例子中 ".woof_container.woof_container_producer" 应该隐藏但是 ".woof_container.woof_container_category" 应该仍然可见。

你能帮我吗?谢谢!

最佳答案

$('.woof_container').each(function(){

  var hide = true;
  
  $(this).find('li').each(function(){
    if(!$(this).hasClass('hide')) {
      hide =false;
    }
  });
  
  if(hide) {
    $(this).remove();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="woof_container woof_container_producer">
 <div class="woof_container_inner">
  <div class="woof_block_html_items">
   <ul class="woof_list">
    <li class="woof_term hide">Nike<span class="quantity">(0)</span></li>
    <li class="woof_term hide">Reebok<span class="quantity">(0)</span></li>
    <li class="woof_term hide">Adidas<span class="quantity">(0)</span></li>
    <li class="woof_term hide">Puma<span class="quantity">(0)</span></li>
   </ul>
  </div>
 </div>
</div>

<div class="woof_container woof_container_category">
 <div class="woof_container_inner">
  <div class="woof_block_html_items">
   <ul class="woof_list">
    <li class="woof_term hide">Jacket<span class="quantity">(0)</span></li>
    <li class="woof_term">T-shirt<span class="quantity">(3)</span></li>
    <li class="woof_term hide">Dress<span class="quantity">(0)</span></li>
    <li class="woof_term hide">Boots<span class="quantity">(0)</span></li>
   </ul>
  </div>
 </div>
</div>

关于javascript - 当所有 child 都有特定类(class)时隐藏父 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48112550/

相关文章:

javascript - Dojo 将索引 0 处的数据从数组绑定(bind)到小部件

javascript - 尝试在 Discord 机器人中发送随机消息 (javascript)

javascript - 浏览器自动关闭列表标签

javascript - 刷新时 jQuery 切换状态发生变化

html - CSS HTML 在图像上放置一个白色方 block

javascript - 如何将元素自动放置在彼此之下

javascript - 如何解决 "Cannot call method "addL"of undefined"

javascript - 如何在 Codeigniter 中使用 jquery Ajax 在行中添加新的动态行?

html - 如何为页面主体(html)留边距?

新 Aptana Studio 3 上的 jQuery