【问题标题】:Limit jQuery search to specific containers like h and p tags将 jQuery 搜索限制为特定容器,例如 h 和 p 标签
【发布时间】:2019-07-17 15:44:18
【问题描述】:

我有这个带有搜索过滤器的词汇表页面设计。我刚刚注意到过滤器基于所有 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();  
        }

    });
});

【问题讨论】:

    标签: javascript jquery filtering


    【解决方案1】:

    您可以在使用.html() 之前使用find (.find("p,h1,h2,h3,h4,h5")) 函数。

    【讨论】:

    • @KYSSE 很高兴我能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 2021-01-29
    • 2016-10-26
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多