我有一个带有搜索过滤器的词汇表页面设计。我刚刚注意到过滤器是根据所有 html 进行过滤,而不仅仅是标题和 p 标签。例如,如果放置一个文件名为 hello.jpg 的图像,并且我搜索了 hello。它也会过滤掉它。所以它读取所有的html。我只是希望它根据 H 标签和 P 标签或仅我设置的特定标签/类进行过滤。不管怎样都比较容易。
任何帮助将不胜感激。
这是我到目前为止的脚本
$("#txtSearch").on('keyup', function() {
var search = $(this).val().toLowerCase();
//Go through each list item and hide if not match search
$(".list-group-item").each(function() {
if ($(this).html().toLowerCase().indexOf(search) != -1) {
$(this).show();
}
else {
$(this).hide();
}
});
});
最佳答案
您可以在使用 .html 之前使用
.find
(.find("p,h1,h2,h3,h4,h5")
) 函数()
关于javascript - 将 jQuery 搜索限制为特定容器,例如 h 和 p 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57069998/