Javascript 清除搜索字段时如何隐藏项目?
我想在清除搜索字段时隐藏项目,但在搜索项目并清除搜索字段时,所有项目都将可见 我试过了Javascript 清除搜索字段时如何隐藏项目?,javascript,php,html,Javascript,Php,Html,我想在清除搜索字段时隐藏项目,但在搜索项目并清除搜索字段时,所有项目都将可见 我试过了 if(input.value.length == 0){ a[i].style.display = "none"; return; }else{ a[i].style.display = "block"; } 但这会产生一个错误: index.php:160unc
if(input.value.length == 0){
a[i].style.display = "none";
return;
}else{
a[i].style.display = "block";
}
但这会产生一个错误:
index.php:160
uncaughttypeerror:无法读取未定义的属性“style”
在filterFunction(index.php:160
)
在HTMLInputElement.onkeyup(index.php:125
)
第125行:
第160行:filter=input.value.toUpperCase()代码>
代码如下:
<div class="dropdown">
<div id="myDropdown" class="dropdown-content">
<input type="text" placeholder="Search.."
id="myInput" onkeyup="filterFunction()">
<a href="Index.html">Home</a>
<a href="">Refurbish Desktop</a>
<a href="">Refurbish Laptop</a>
<a href="">Refurbish Monitoren</a>
<a href="">Refurbish Printers</a>
<a href="">Groothandel Desktop</a>
<a href="">Groothandel Laptop</a>
<a href="">Groothandel Monitoren</a>
<a href="">Groothandel Printers</a>
<a href="">Recycling Desktop</a>
<a href="">Recycling Laptop</a>
<a href="">Recycling Thin Client</a>
<a href="">Recycling Monitoren</a>
<a href="">Recycling Printers</a>
<a href="">Werkproces HP Folio 9470m</a>
<a href="">QA Refurbish Desktop</a>
<a href="">QA Refurbish Laptop</a>
<a href="">QA Refurbish Monitoren</a>
<a href="">QA Refurbish Printers</a>
</div>
</div>
<script>
function filterFunction() {
var input, filter, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
div = document.getElementById("myDropdown");
a = div.getElementsByTagName("a");
if(input.value.length == 0){
a[i].style.display = "none";
return;
}else{
a[i].style.display = "block";
}
for (i = 0; i < a.length; i++) {
txtValue = a[i].textContent || a[i].innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
a[i].style.display = "block";
} else {
a[i].style.display = "none";
}
}
}
</script>
函数filterFunction(){
变量输入,过滤器,a,i;
输入=document.getElementById(“myInput”);
filter=input.value.toUpperCase();
div=document.getElementById(“myDropdown”);
a=div.getElementsByTagName(“a”);
if(input.value.length==0){
a[i].style.display=“无”;
返回;
}否则{
a[i].style.display=“block”;
}
对于(i=0;i-1){
a[i].style.display=“block”;
}否则{
a[i].style.display=“无”;
}
}
}
我希望在清除字段时清除该字段您第一次没有定义I
试试这个:
if(input.value.length == 0){
for(i=0;i<a.length;i++){
a[i].style.display = "none";
}
return;
}
else{
for(i=0;i<a.length;i++){
a[i].style.display = "block";
}
}
if(input.value.length==0){
对于(i=0;i您在输入标记上使用onkeyup
事件。您是否可以尝试onkeyup
而不是onkeyup
<input type="text" placeholder="Search.." id="myInput" onkeydown="filterFunction()">
Hi@IstiaqueHossain老实说我不知道,这是我第一次使用java(javascript)谢谢,我会研究这个!Hi这很有效,非常感谢,我以为我已经为(i=0;i-1)定义了i
Hi@ariferol01上面的评论修复了我的问题。不过还是谢谢你。