繁体   English   中英

使用 .each 的 Jquery 搜索过滤器

[英]Jquery search filter using .each

因此,我正在尝试编写具有显示和隐藏功能的搜索过滤器。 但是我不知道在 .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
         }
    }); 
});

尝试这个:

var $pictureList = $("#gallery img");
$("#search").onkeypress(function(event) {
    var re = new RegExp($(this).val());
    $pictureList.each(function() {
        var self = $(this);
        if (self.attr('alt').match(re)) {
            // image match
        }
     }); 
});

如果搜索框中的值可以包含正则表达式字符,您可能希望先对其进行转义

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM