如何检查图像是否有属性

How to check if image has attribute?

本文关键字:图像 是否 属性 检查 何检查      更新时间:2023-09-26

我有img标签的列表,其中一些具有属性style="display:none"例如:

<img src="#">
<img src="#" style="display: none">
<img src="#">
<img src="#" style="display: none">
<img src="#" style="display: none">
<img src="#">
<img src="#" style="display: none">

如何(使用jQuery或javascript)获取没有属性style="display:none"的元素?

UPD:我选择了具有某些来源($("img[src='http://certain-source.jpg']"))的图像,但其中一些具有字符串style="display:none",现在我必须选择不包含该字符串的图像。

检查图像是否可见:

$('img:visible')

将是最简单的,检查该确切属性是另一个

$('img[style!="display: none"]')

基于样式的过滤也是一种选择

$('img').filter(function() {
    return this.style.display != 'none';
})

可能还有一百种其他解决方案?

@adeneo给出了一个很好的答案,

您也可以尝试使用过滤器

$("img").filter(function() {
 return $(this).css("display") == "none"});