简体   繁体   English

使用vanilla JS检测是否所有孩子都被隐藏并隐藏父div

[英]Detect if all children are hidden and hide parent div using vanilla JS

As the title suggests, I wwant to build a filter that hides elements. 如标题所示,我想构建一个隐藏元素的过滤器。 If all children are hidden then the paretn should also set itself to display none only I can't quite figure out how to approach it. 如果所有孩子都被隐藏了,那么paretn还应该设置为不显示任何内容,因为我无法弄清楚该如何处理。 I've tried the below with no luck. 我没有运气尝试过下面的方法。

https://jsfiddle.net/6wt0jndp/1/ https://jsfiddle.net/6wt0jndp/1/

function filter(e){
  search = e.value.toLowerCase();
  console.log(e.value)
  document.querySelectorAll('.kb-item').forEach(function(row){
     text = row.innerText.toLowerCase();
     if(text.match(search)){
        row.style.display="block"
     } else {
        row.style.display="none"
     }

      // need to count hidden items and if all instances of .kb-items are hidden, then hide .kb-item
     var countHidden = document.querySelectorAll(".kb-item[style='display: none;']").length;
     var children = document.querySelectorAll(".kb-items").length;

     if(countHidden > children){
       row.style.display="none"
     }

      console.log(countHidden);
  })
}

HTML HTML

<div class="kb-items">
  <h1>fruits</h1>
  <div class="kb-item">apple</div>
  <div class="kb-item">banana</div>
</div>

<div class="kb-items">
  <h1>vegetables</h1>
  <div class="kb-item">lettuce</div>
  <div class="kb-item">onion</div>
  <div class="kb-item">carrot</div>
</div>

You need to do things to parent after iterating children, not during doing it. 您需要在迭代孩子之后而不是在做过程中向父母做事。

  1. Get children for each parent they have, not whole .kb-item elements. 为每个父母(而不是整个.kb-item元素)获取孩子。

  2. Compare the length of children and length of them with display: none style. 比较孩子的长度和他们的长度与display: none样式。

function filter(e) {
  // ...
  var parents = document.querySelectorAll(".kb-items");
  parents.forEach(parent => {
    if (parent.querySelectorAll(".kb-item").length == parent.querySelectorAll(".kb-item[style='display: none;']").length) {
      parent.style.display = "none"
    } else {
      parent.style.display = "block"
    }
  })

Complete code snippet : 完整的代码段

 function filter(e) { search = e.value.toLowerCase(); console.log(e.value) document.querySelectorAll('.kb-item').forEach(function(row) { text = row.innerText.toLowerCase(); if (text.match(search)) { row.style.display = "block" } else { row.style.display = "none" } }) var parents = document.querySelectorAll(".kb-items"); parents.forEach(parent => { if (parent.querySelectorAll(".kb-item").length == parent.querySelectorAll(".kb-item[style='display: none;']").length) { parent.style.display = "none" } else { parent.style.display = "block" } }) } 
 <input type="text" onkeyup="filter(this)" /> <div class="kb-items"> <h1>fruits</h1> <div class="kb-item">apple</div> <div class="kb-item">banana</div> </div> <div class="kb-items"> <h1>vegetables</h1> <div class="kb-item">lettuce</div> <div class="kb-item">onion</div> <div class="kb-item">carrot</div> </div> 

may be you can trigger two function on keypress . 可能是您可以在按键上触发两个功能。 One would detect the elements state in a group which will detect whether all the element is hidden or not. 一个人将检测一组中的元素状态,该状态将检测所有元素是否被隐藏。 We can edit the below code a bit for dom efficency. 我们可以编辑以下代码以提高dom效率。

Hope this is the result what you are expecting fiddle link : https://jsfiddle.net/binaryslate/2z1ptnao/1/ 希望这是您期望的结果小提琴链接: https : //jsfiddle.net/binaryslate/2z1ptnao/1/

        <input type="text" onkeyup="filter(this);detectParent();"/>

        function detectParent()
        {
            var collectionref=document.querySelectorAll(".kb-items");
            collectionref.forEach(group=>{
            var itemcollection=group.getElementsByClassName("kb-item");
            var hidecounter=0;
            for(var j=0;j<itemcollection.length;j++)
            {

                if(itemcollection[j].style.display==='none')
                {
                    hidecounter++;
                }
            }
            if(hidecounter===itemcollection.length)
            {
                group.style.display="none";
            }else{
                group.style.display="block";
            }

            });
        }

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

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