[英]Jquery search filter using .each
So I am trying to code a search filter with show and hide features.因此,我正在尝试编写具有显示和隐藏功能的搜索过滤器。 But I have no clue what to do next after .each function.
但是我不知道在 .each 函数之后接下来要做什么。 to make the search actually search through my "alt" attribute under img tag.
使搜索实际搜索 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
});
});
Thank you!谢谢!
You can use the attr
function of the jQuery to get the alt attribute for each element:您可以使用 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
}
});
});
Try this:尝试这个:
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
}
});
});
if value in search box can contain regex characters you may want to escape it first .如果搜索框中的值可以包含正则表达式字符,您可能希望先对其进行转义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.