[英]jquery/javascript - hide div if containing image is broken
i have a div with the class 'userFeatureProductImage'. 我有一个类'userFeatureProductImage'的div。 this div is repeated however occasionally a broken image might come through so i want to hide the div as a precaution if this does happen.
这个div是重复的,但偶尔会有一个破碎的图像可能会通过,所以我想隐藏div作为预防措施,如果确实发生这种情况。 how can i do this?
我怎样才能做到这一点?
The load event is only fired if an image is valid: 只有在图像有效时才会触发load事件:
$('.userFeatureProductImage').hide();
$('.userFeatureProductImage img').load(function() {
$(this).closest('.userFeatureProductImage').show();
});
Use the error event which is called when an error occurs with the image: 使用图像发生错误时调用的错误事件:
$(".userFeatureProductImage img").error(function(){
$(this).parent().hide();
});
Example: http://jsfiddle.net/jonathon/vWwpc/ 示例: http : //jsfiddle.net/jonathon/vWwpc/
You might try something like this: 你可能会尝试这样的事情:
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
function hideBrokenImage(id) {
$(id.+"userFeatureProductImage").hide();
}
</script>
<div id="imageFilename" class="userFeatureProductImage">
<img src="imageFilename.gif" onerror="hideBrokenImage(imageFilename);" />
</div>
Where imageFilename is obviously dynamically created. 其中imageFilename显然是动态创建的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.