我在搜索栏中有问题。 在一个页面中我至少有 30 个产品。 我会添加过滤器,当客户搜索产品名称时,产品显示。
我尝试创建过滤器,但无法编写正确的结构。 有没有人可以帮助我,我变得快乐。
如果 html 列表中列出了产品,您可以尝试以下代码
function myFunction() {
// Declare variables
var input, filter, ul, li, a, i, txtValue;
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];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}