How can I select in jQuery all the div
s that have background-image: url(somepath/somename.png)
in their style?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
There isn't a jQuery selector, but this might work:
$('div').each( function() {
if ( $(this).css('background-image') == 'url("image.png")' ) {
// do something here
}
});
However, a more efficient method would be to make sure you only have a single class that uses that background image, then simply select $('.bgClass')
回答2:
Try adding a custom selector:
$(document).ready(function() {
$.extend($.expr[':'], {
hasMyImage: function(el) {
return ($(el).css('background-image') == "Url('somepath/somename.png')");
}
});
});
Then to select:
$("div:hasMyImage");
回答3:
Use the filter function:
var matches = $("div").filter(function()
{
return ($(this).css("background-image") == "url('somepath/somename.png')");
});