jQuery 找到 :contains?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4498177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:21:53  来源:igfitidea点击:

find image src that :contains?

jquerycontainssrc

提问by Charles Web Dev

Morning all,

大家早,

I have a list of images like so:

我有一个像这样的图像列表:

<ul id="preload" style="display:none;">
<li><img src="afx4000z-navy-icon-1_thumb.jpg"/></li>
<li><img src="afx4000z-green-icon-1_thumb.jpg"/></li>
</ul>

Using jQuery how find all image src's within ul#preload that contain a specific string eg."green"

使用 jQuery 如何在 ul#preload 中查找包含特定字符串的所有图像 src,例如“green”

something like...

就像是...

var new_src = jQuery('#preload img').attr('src')*** that contains green ***;

回答by Gabi Purcaru

You need to use the *=selector:

您需要使用*=选择

jQuery('#preload img[src*="green"]')

If you want it to be case insensitive, it will be a bit more difficult:

如果您希望它不区分大小写,则会有点困难:

var keyword = "green";
$("#preload img").filter(function(keyword) {
    return $(this).attr("src").toLowerCase().indexOf(keyword.toLowerCase()) != -1;
});

回答by Nick Craver

You can use an attribute-contains selector([attr*=value]), like this:

您可以使用包含属性的选择器( [attr*=value]),如下所示:

jQuery('#preload img[src*=green]')