简体   繁体   English

如何在整个 div 上制作搜索过滤器

[英]How to make search filter on entire div

I have HTML code like this in my project我的项目中有这样的 HTML 代码

        <div class="container" id="containerdiv">
        <div id="rollingdiv">
        <div class="row">
            <div class="col-3" id="columndiv">
                <a href="https://www.linkedin.com/in/arun7kumar/">
                <div>
                    <img class="icontrial" src="img/iAngelsmugshotsblackandwhite/ArunKumar.jpeg" alt="">
                </div>
                <div class="texttrial">
                    <p>Arun Kumar</p>
                    <p>Technology</p>
                 </div>   
                </a>
             </div>
            <div class="col-3" id="columndiv">
                <a href="https://www.linkedin.com/in/meeraiyer/">
                <div>
                    <img class="icontrial" src="img/iAngelsmugshotsblackandwhite/MeeraIyer.jpeg" alt="">
                </div>
                <div class="texttrial">
                    <p>Meera Iyer</p>
                    <p>Marketing & Communication</p>
                 </div>  
                 </a>
            </div>
            <div class="col-3" id="columndiv">
                <a href="www.linkedin.com/in/prashant-rohatgi-47b6472">
                <div>
                    <img class="icontrial" src="img/iAngelsmugshotsblackandwhite/PrashantRohtagi.jpeg" alt="">
                </div>
                <div class="texttrial">
                    <p>Prashant Rohtagi</p>
                    <p>Digital Transformation</p>
                 </div>
                 </a>
            </div>
            <div class="col-3" id="columndiv">
                <a href="www.linkedin.com/in/richa-arora-05127aa">
                <div>
                    <img class="icontrial" src="img/iAngelsmugshotsblackandwhite/RichaArora.jpeg" alt="">
                </div>
                <div class="texttrial">
                    <p>Richa Arora</p>
                    <p>Sales</p>
                 </div> 
                 </a>
            </div>
        </div>

The div=row onwards keeps continuing for about 10 rows. div=row 继续持续大约 10 行。 Making 10 rows and 40 such folks with image and name displayed on screen.制作 10 行和 40 个这样的人,图像和名称显示在屏幕上。

I want to make the name which is the first我想取第一个名字

tag in the row searchable.可搜索行中的标记。 When there is a match, only the div with the id "columndiv" should pop up.当匹配时,应该只弹出 id 为“columndiv”的 div。 So that only the div for the searched person gets displayed.这样只会显示搜索到的人的 div。

My function looks like this我的功能看起来像这样

<script>
    

    function myFunction() {
          var input, filter, ul, li, a, i, txtValue;
          var divValues = []
          input = document.getElementById('myInput');
          filter = input.value.toUpperCase();
          ul = document.getElementById("containerdiv");
          li = ul.getElementsByTagName('p');
          console.log(li)
        
          // Loop through all list items, and hide those who don't match the search query
          for (i = 0; i < li.length; i++) {
            console.log("hi i am inside")
            a=li[i]
            txtValue = a.textContent || a.innerText;
            if (txtValue.toUpperCase().indexOf(filter) > -1) {
              li[i].style.display = "";
            } else {
              li[i].style.display = "none";
            }
          }
}
</script>

With the above, only the综上所述,只有

tag with the name is searchable.带有名称的标签是可搜索的。 However I want the searched div, with the name, image displayed.但是我想要搜索的 div,显示名称和图像。

How do I go about doing this我该怎么做

You are changing the display value for each p-tag and not for the whole div.您正在更改每个 p-tag 的显示值,而不是整个 div。 Just run through all DIVs instead of all Ps:只需运行所有 DIV 而不是所有 P:

function myFunction() {
      var input, filter, ul, li, a, i, txtValue;
      input = document.getElementById('myInput');
      filter = input.value.toUpperCase();
      ul = document.getElementById("containerdiv");
      li = ul.getElementsByClassName('col-3'); //<<<<<<<<<<<<<<<< Change this line
    
      // Loop through all list items, and hide those who don't match the search query
      for (i = 0; i < li.length; i++) {
        a=li[i]
        txtValue = a.textContent || a.innerText;
        if (txtValue.toUpperCase().indexOf(filter) > -1) {
          li[i].style.display = "";
        } else {
          li[i].style.display = "none";
        }
      }
    }

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

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