【发布时间】:2012-12-05 19:24:43
【问题描述】:
我有一个搜索功能,可以很高兴地通过列表 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);
}
});
【问题讨论】:
-
你的意思是例如
if( $(this).attr('data-attr1').search(..) || $(this).attr('data-attr2').search(..) ) {} else {}?我只是提出建议,也许甚至可以做到[ $(this).attr('data-attrib1'), $(this).attr('data-attr2'), $(this).attr('data-attr3') ].search(..)
标签: javascript jquery search attr