0
所以我使用這個如何from w3schools,但我想搜索整個<li>
而不僅僅是<a>
標籤。我可以看到這是問題a = li[i].getElementsByTagName("a")[0];
但我無法弄清楚如何改變它?Sortable/Searchable List with Javascript
這裏是他們的標記:
<ul id="myUL">
<li><a href="#">Adele</a></li>
<li><a href="#">Agnes</a></li>
</ul>
但我想這一點:
<input type="text" id="myInput" onkeyup="myFunction()"
placeholder="Search for names..">
<ul id="myUL">
<li><a href="#">Adele</a> - Some more text that gets picked up by
search.</li>
<li><a href="#">Agnes</a> - Some more text that gets picked up by
search.</li>
</ul>
而這裏的腳本:
<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>