我有列表格式的按钮。我希望能够使用此搜索脚本或其他搜索脚本搜索按钮。它目前无法使用此代码。
fp
function myFunction() {
var input, filter, ul, li, a, i;
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];
if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}
有没有人有脚本可以让我搜索这些按钮?
答案 0 :(得分:2)
在li
项中,没有a
个元素。
只需更改此行:
a = li[i].getElementsByTagName("a")[0];
为:
a = li[i].getElementsByTagName("button")[0];
function myFunction() {
var input, filter, ul, li, a, i;
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("button")[0];
if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Please type a keyword." title="FAQ Search">
<ul id="myUL">
<li><button onclick="document.getElementById('id01').style.display='block'" class="w3-btn w3-white w3-hide-small"><i class="fa fa-info w3-margin-center"></i>How To Use This Site</button>
<div id="id01" class="w3-modal">
<div class="w3-modal-content">
<div class="w3-container">
<span onclick="document.getElementById('id01').style.display='none'" class="w3-closebtn">×</span>
<p>Hello</p>
</div>
</div>
</div>
</li>
<li><button onclick="document.getElementById('id02').style.display='block'" class="w3-btn w3-white w3-hide-small"><i class="fa fa-info w3-margin-center"></i>When to use</button>
<div id="id02" class="w3-modal">
<div class="w3-modal-content">
<div class="w3-container">
<span onclick="document.getElementById('id02').style.display='none'" class="w3-closebtn">×</span>
<p>Hello</p>
</div>
</div>
</div>
</li>
</ul>
P.S。当然,考虑到它不是a
元素,将变量的名称更改为有意义的东西也会很好......