find image src that :contains?

两盒软妹~` 提交于 2019-12-05 08:37:51

问题


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"

something like...

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

回答1:


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;
});



回答2:


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

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


来源:https://stackoverflow.com/questions/4498177/find-image-src-that-contains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!