基于.attr()和.text()筛选列表

s1ag04yj  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(163)

因此,我使用.each()创建了一个包含基本文本筛选器的列表,它正在工作,但是我希望根据自定义属性(数据标记)或链接文本内容中的匹配项进行筛选。见下文。。

$(document).on("input", "#find-link", function() {
    // Get the value of the textbox
    var linkSearchValue = $(this).val().toLowerCase();
    // Cycle through the list of links
    $(".link").each(function(i) {
        // If the searchbox is NOT empty
        if (linkSearchValue != "") {
           // Get the tags and text to compare against
           var linkTags = $(this).attr("data-tag");
           var linkText = $(this).text();
           if ( (!linkTags.toLowerCase().includes(linkSearchValue)) || (!linkText.toLowerCase().includes(linkSearchValue)) ) { // Check if the search string DOESN'T match...
               // If there isn't a match for a link, hide it.
               $(this).addClass("hidden-link");
           } else {
               // If there IS a match for a link, show it.
               $(this).removeClass("hidden-link");
           }
        } else {
           $(this).removeClass("hidden-link");
        }
    });
});

在节中

if ( (!linkTags.toLowerCase().includes(linkSearchValue)) || (!linkText.toLowerCase().includes(linkSearchValue)) ) { // Check if the search string DOESN'T match...

如果我只检查链接标签,或者只检查链接文本,但不能同时检查两者,它就可以正常工作。。。如果我同时选中这两个,它只会选中链接标签。
为什么它不同时检查这两个呢?

x9ybnkn6

x9ybnkn61#

使用 && 而不是 || 如@barmar在评论中解释的,解决了该问题。从他们发布的demorgan定律链接中。。。
非(a或b)=(非a)和(非b)
非(a和b)=(非a)或(非b)
因为我希望其中一个表达式不包含搜索的单词,所以我会选择使用第一种格式(因为它不是a或b,所以我会搜索非a和非b)。
这仍然有点令人困惑,但由于没有给出答案(只是评论),并且评论解决了问题,我将把它作为一个答案。

相关问题