gpt4 book ai didi

javascript - 在搜索框中收到输入之前,如何隐藏列表结果?

转载 作者:行者123 更新时间:2023-11-28 02:33:15 25 4
gpt4 key购买 nike

function myFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
ul = document.getElementById("myUL");
li = ul.getElementsByTagName("li");

for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";

}
}
}
* {
box-sizing: border-box;
}

#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 12px;
background-repeat: no-repeat;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}

#myUL {
list-style-type: none;
padding: 0;
margin: 0;
}

#myUL li a {
border: 1px solid #ddd;
margin-top: -1px; /* Prevent double borders */
background-color: #f6f6f6;
padding: 12px;
text-decoration: none;
font-size: 18px;
color: black;
display: block
}

#myUL li a:hover:not(.header) {
background-color: #eee;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<h2>Branch Email</h2>

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Enter your branch code..." title="Type in a name">

<ul id="myUL">
<li><a href="#">Adele</a></li>
<li><a href="#">Agnes</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>



</body>
</html>

如何修改我的代码,以便在用户搜索某些内容之前不显示列表项?

最佳答案

隐藏所有 li 的。默认

#myUL li{
display:none;
}

并且在 input 中的按键将它们的显示更改为 block 而不是 ""

if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "block";
} else {
li[i].style.display = "none";
}

function myFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
ul = document.getElementById("myUL");
li = ul.getElementsByTagName("li");

for (i = 0; i < li.length; i++) {

a = li[i].getElementsByTagName("a")[0];
if (filter!== "" && a.innerHTML.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "block";
} else {
li[i].style.display = "none";
}
}
}
* {
box-sizing: border-box;
}

#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 12px;
background-repeat: no-repeat;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}

#myUL {
list-style-type: none;
padding: 0;
margin: 0;
}

#myUL li a {
border: 1px solid #ddd;
margin-top: -1px;
/* Prevent double borders */
background-color: #f6f6f6;
padding: 12px;
text-decoration: none;
font-size: 18px;
color: black;
display: block
}

#myUL li {
display: none;
}

#myUL li a:hover:not(.header) {
background-color: #eee;
}
<!DOCTYPE html>
<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>

<h2>Branch Email</h2>

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Enter your branch code..." title="Type in a name">

<ul id="myUL">
<li><a href="#">Adele</a></li>
<li><a href="#">Agnes</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>



</body>

</html>

更新:

if条件更新为

filter!== "" && a.innerHTML.toUpperCase().indexOf(filter) > -1

处理退格并清空输入值的情况。

关于javascript - 在搜索框中收到输入之前,如何隐藏列表结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49092798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com