javascript - 根据 .attr() 和 .text() 过滤列表
问题描述
因此,我使用 .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...
如果我检查 JUST linkTags 或 JUST linkText,但不是两者都检查,它工作正常......如果我检查两者,它只检查 linkTags。
为什么不检查两者?
解决方案
正如@Barmar 在评论中解释的那样,使用&&
而不是解决问题。||
从他们为德摩根定律发布的链接中获取......
- 非(A 或 B)=(非 A)和(非 B)
- 非(A 和 B)=(非 A)或(非 B)
因为我正在寻找不包含搜索词的 EITHER 表达式,所以我会选择使用第一种格式(因为它不是 A 或 B,所以我会搜索 NOT A和NOT B)。
这仍然有点令人困惑,但由于没有给出答案(只是评论),并且评论解决了这个问题,我将它作为答案。
推荐阅读
- javascript - 通过 Slider 更改字体大小
- html - 仅使用 SMIL 显示动画后的 SVG 条形图值
- bash - Shell 脚本 - 需要帮助来解析包含 IP 地址列表的文件
- workflow-foundation - 如何在 Rehosted 设计器中迭代工作流序列时将输入传递给条件(例如“If”)
- android - 如何从 RecyclerView 项目中将两组数据保存在 Room 数据库中
- jquery - 如何在单击按钮同时选择两个下拉选项时显示隐藏 div
- php - 即使条件错误,Laravel Eloquent 模型也会返回值
- r - 为什么偏差表分析的 p 值与 pchisq() 估计的不同?
- android - JobService 在调用 FirebaseJobDispatcher.cancel() 后不会停止
- java - 在测试中的 Spring Environment 中使用带有 spring.profiles.active 属性的 @EnabledIf