我有一个搜索功能,可以很高兴地通过列表 div 搜索一个数据属性,下面是正在工作的代码。
$(".classToSearch").each(function(){
if ($(this).attr('data-attribute1').search(new RegExp(filter, "i")) < 0) {
$(this).animate({opacity:0.1},100);
} else {
$(this).animate({opacity:0.5},100);
}
});
我希望搜索功能能够搜索多个数据属性。我尝试了一系列不同的格式,但我无法让它工作。下面是我认为它应该看起来的样子。
$(this).attr('data-attribute1','data-attribute2','data-attribute3')
或者
$(this).attr('data-attribute1'||'data-attribute2'||'data-attribute3')
但我想我需要某种 for 循环。任何帮助,将不胜感激。
- - - - - 编辑 - - - - - - -
我 的解决方案 这允许搜索框搜索所有数据属性。
$(".classToSearch").each(function(){
if ($(this).attr('data-attribute1').search(new RegExp(filter, "i")) < 0 &&
$(this).attr('data-attribute2').search(new RegExp(filter, "i")) < 0 &&
$(this).attr('data-attribute3').search(new RegExp(filter, "i")) < 0 &&
) {
$(this).animate({opacity:0.1},100);
} else {
$(this).animate({opacity:0.5},100);
}
});