0
我有一些簡單的代碼,允許過濾列表項目,它幾乎完美的作品。唯一的問題是搜索框只搜索第一個UL,我該如何搜索它們?簡單的JavaScript過濾器/搜索多個列表,而不是隻有一個?
的Javascript:
<script>
function myFunction() {
// Declare variables
var input, filter, ul, li, a, i;
input = document.getElementById('myInput');
filter = input.value.toUpperCase();
ul = document.getElementById("myUL");
li = ul.getElementsByTagName('li');
// 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].getElementsByTagName("a")[0];
if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}
</script>
HTML:
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="search">
<ul id="myUL" class="ul1">
<li><a href="#">bob</a></li>
<li><a href="#">rob</a></li>
<li><a href="#">tom</a></li>
<li><a href="#">mark</a></li>
</ul>
<ul id="myUL" class="ul2">
<li><a href="#">purse</a></li>
<li><a href="#">cat</a></li>
<li><a href="#">pencil</a></li>
<li><a href="#">sharpner</a></li>
</ul>