如果文本框为空,我无法隐藏myUL li
元素。有什么建议吗?先谢谢了。
<input type="text" id="myInput" onKeyUp="myFunction()" placeholder="Just begin typing.." title="Detail Index">
<ul id="myUL">
<li><a href="/year/2000.html" target="_blank">2000</a></li>
<li><a href="/year/2001.html" target="_blank">2001</a></li>
<li><a href="/year/2002.html" target="_blank">2002</a></li>
</ul>
<script>
function myFunction() {
var input, filter, ul, li, a, i, txtValue;
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];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}
</script>
display: none
和display: list-item
之间切换。不过我更喜欢使用el.hidden
。 - connexo