我试图弄清楚为什么这个jQuery在Chrome和IE中工作正常,但在Firefox中却不能。它应该做的(以及在chrome和IE中正在做的)是遍历ul中的每个li,看看IMG src是否抛出错误。如果是这样,则删除 li。
$('ul.community-properties li').each(function() {
$('li IMG').on('error', function() {
$(this).parent().remove();
});
});
我也试过
$('ul.community-properties li').each(function() {
$('li IMG').error(function() {
$(this).parent().remove();
});
});
如果有人有更有效的方法或方法可以让它在所有浏览器中工作,那就太好了。
这是 html 结构
<ul class="community-properties">
<li>
<img src="{tag_community property1_value}" />
<div class="item-link"><a href="{tag_community property1 url}">View Property Details</a></div>
</li>
<li>
<img src="{tag_community property2_value}" />
<div class="item-link"><a href="{tag_community property2 url}">View Property Details</a></div>
</li>
<li>
<img src="{tag_community property3_value}" />
<div class="item-link"><a href="{tag_community property3 url}">View Property Details</a></div>
</li>
<li>
<img src="{tag_community property4_value}" />
<div class="item-link"><a href="{tag_community property4 url}">View Property Details</a></div>
</li>
</ul>
检查图像是否已加载
window.setTimeout( function() { //added delay to make sure that onerror already has run.
$('ul.community-properties li img').one('error', function() { //bind the error event to the images
$(this).parent().remove();
}).filter( function() { //check to see if an image was loaded from the cache as broken (or if onerror fired before set)
return (this.complete && (this.naturalWidth===0 || this.naturalWidth===undefined));
}).trigger("error"); //fire the error event
},1000); //Broken images should hide after one second
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul class="community-properties">
<li>
<img src="{tag_community property1_value}" />
<div class="item-link"><a href="{tag_community property1 url}">View Property Details</a></div>
</li>
<li>
<img src="http://i.imgur.com/UyY6HH5.gif" />
<div class="item-link"><a href="{tag_community property2 url}">View Property Details</a></div>
</li>
<li>
<img src="{tag_community property3_value}" />
<div class="item-link"><a href="{tag_community property3 url}">View Property Details</a></div>
</li>
<li>
<img src="{tag_community property4_value}" />
<div class="item-link"><a href="{tag_community property4 url}">View Property Details</a></div>
</li>
</ul>