1 回答
TA贡献1817条经验 获得超6个赞
您可以采用另一个变量并将其设置为true并迭代过滤词并在最后关闭不需要的项目。
match = true;
for (var f = 0; f < filter.length && match; f++) { // exit loop if not match
match = txtValue.includes(filter[f]);
}
li[i].style.display = match ? '' : 'none';
function myFunction() {
var filter = $('input').val().toUpperCase().split(/\s+/);
var li = $('li');
var a = $('a');
var ul;
var txtValue;
ul = document.getElementById("myUL");
var match
for (var i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
txtValue = (a.textContent || a.innerText).toUpperCase();
match = true;
for (var f = 0; f < filter.length && match; f++) {
match = txtValue.includes(filter[f]);
}
li[i].style.display = match ? '' : 'none';
}
}
<style>* {
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;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">
<ul id="myUL">
<li><a href="#">Adele Correy</a></li>
<li><a href="#">Agnes Green</a></li>
<li><a href="#">Billy Correy</a></li>
<li><a href="#">Bob Green</a></li>
<li><a href="#">Calvin Green</a></li>
<li><a href="#">Christina Correy</a></li>
<li><a href="#">Cindy Correy</a></li>
</ul>
- 1 回答
- 0 关注
- 91 浏览
添加回答
举报