[英]Hide elements when searching for <li> inside a <ul>
I have this code where I have an input and I can search for <li>
elements inside a <ul>
.我有这个代码,我有一个输入,我可以在
<ul>
搜索<li>
元素。 It's working like a charm, however, I also have "button" (that will work as a title and accordion in the future) elements inside the same div as the <ul>
and when I filter the <li>
elements the button remains (obviously)它就像一个魅力,但是,我在与
<ul>
相同的 div 中也有“按钮”(将来将用作标题和手风琴)元素,当我过滤<li>
元素时,按钮仍然保留(明显地)
I want the button to also disappear when I hide the <li>
elements that I don't want to see.当我隐藏不想看到的
<li>
元素时,我希望按钮也消失。 For example, if I search for "Adele" I also want to hide the button "D TO F", since it's an element from a <ul>
I'm not using, meaning I want all the div to disappear.例如,如果我搜索“Adele”,我还想隐藏按钮“D TO F”,因为它是来自
<ul>
的一个元素,我没有使用,这意味着我希望所有 div 消失。 (tried to be as clear as possible). (试图尽可能清楚)。
Any help would be appreciated!任何帮助,将不胜感激! Code below:
代码如下:
function myFunction() { var input, filter, ul, li, a, i, txtValue; input = document.getElementById("myInput"); filter = input.value.toUpperCase(); // get all of the li elements that are contained by "myUL" elements document.querySelectorAll(".myUL li") // iterate over them .forEach(l => l // toggle the "hidden" class on those li items that have .classList.toggle("hidden", // an anchor tag whose text includes the text typed into the textbox !l.querySelector("a").textContent.toUpperCase().includes(filter))); }
.hidden { display: none; }
<h2>My Phonebook</h2> <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name"> <div class=hide-box> <button>A TO C</button> <ul class="myUL"> <li><a href="#">Adele</a></li> <li><a href="#">Agnes</a></li> <li><a href="#">Anne</a></li> <li><a href="#">Billy</a></li> <li><a href="#">Bob</a></li> <li><a href="#">Calvin</a></li> <li><a href="#">Christina</a></li> <li><a href="#">Cindy</a></li> </ul> </div> <div class=hide-box> <button>D TO F</button> <ul class="myUL"> <li><a href="#">Daniel</a></li> <li><a href="#">Danielle</a></li> <li><a href="#">Ernest</a></li> <li><a href="#">Eric</a></li> <li><a href="#">Fabio</a></li> <li><a href="#">Ferdinand</a></li> <li><a href="#">Frederick</a></li> </ul> </div>
我会给与您要过滤的按钮文本/信息相关的父<div>
的 id,并用您的过滤器隐藏整个 div。
One quick way to fix it is to insert the button to the list.修复它的一种快速方法是将按钮插入到列表中。 Then on the iteration you need to change
a
to *
- meaning all elements inside the list item.然后在迭代中,您需要将
a
更改为*
- 表示列表项内的所有元素。
Something like that:类似的东西:
function myFunction() { var input, filter, ul, li, a, i, txtValue; input = document.getElementById("myInput"); filter = input.value.toUpperCase(); document.querySelectorAll(".myUL li") .forEach(l => l.classList.toggle("hidden",!l.querySelector("*").textContent.toUpperCase().includes(filter)));}
.acoBtn { list-style: none} .hidden { display: none; }
<h2>My Phonebook</h2> <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name"> <div class=hide-box> <ul class="myUL"> <li class="acoBtn"><button>A TO C</button></li> <li><a href="#">Adele</a></li> <li><a href="#">Agnes</a></li> <li><a href="#">Anne</a></li> <li><a href="#">Billy</a></li> <li><a href="#">Bob</a></li> <li><a href="#">Calvin</a></li> <li><a href="#">Christina</a></li> <li><a href="#">Cindy</a></li> </ul> </div> <div class=hide-box> <ul class="myUL"> <li class="acoBtn"><button>D TO F</button></li> <li><a href="#">Daniel</a></li> <li><a href="#">Danielle</a></li> <li><a href="#">Ernest</a></li> <li><a href="#">Eric</a></li> <li><a href="#">Fabio</a></li> <li><a href="#">Ferdinand</a></li> <li><a href="#">Frederick</a></li> </ul> </div>
Just add below snippet :只需添加以下代码段:
document.querySelectorAll(".hide-box")
.forEach(d => d
.classList.toggle("hidden",
([...d.querySelectorAll('.myUL li')]
.every(l => l.classList.contains("hidden"))
)
)
)
in your function making your complete function look like :在您的功能中,使您的完整功能看起来像:
function myFunction() {
var input, filter, ul, li, a, i, txtValue;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
// get all of the li elements that are contained by "myUL" elements
document.querySelectorAll(".myUL li")
// iterate over them
.forEach(l => l
// toggle the "hidden" class on those li items that have
.classList.toggle("hidden",
// an anchor tag whose text includes the text typed into the textbox
!l.querySelector("a").textContent.toUpperCase().includes(filter)));
// Code that checks if all li in the ul of a given div are hidden then hide entire div as well
document.querySelectorAll(".hide-box")
.forEach(d => d
.classList.toggle("hidden",
([...d.querySelectorAll('.myUL li')]
.every(l => l.classList.contains("hidden"))
)
)
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.