所以我正在尝试编写一个具有显示和隐藏功能的搜索过滤器。但我不知道 .each 函数之后下一步该做什么。使搜索实际上通过 img 标签下的“alt”属性进行搜索。
// Search filter should be keypress with alt attribute. *** .each
var $pictureList = $("#gallery img");
$("#search").onkeypress(function(event){
$pictureList.each(function(){
//??? need to go through each "alt" attribute for search filter
});
});
谢谢!
最佳答案
您可以使用 jQuery 的 attr
函数来获取每个元素的 alt 属性:
var $pictureList = $("#gallery img");
$("#search").onkeypress(function(event){
$pictureList.each(function(){
if($(this).attr('alt')=='something'){
//now do what ever you want when it matched the alt attribute
}
});
});
关于javascript - 使用 .each 的 Jquery 搜索过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37510689/