jQuery('img').bind('error',function(){
alert('hi');
jQuery(this).hide();
});
我已经写了这段代码,但是不可用的图像没有隐藏并且仍然显示十字符号。任何人都可以指出什么可能是错的。我在 document.ready 下写这个,我也在 window.onload 下试过。
最佳答案
问题似乎是当您绑定(bind)错误事件时,图像的 onerror 已经触发。您可以在绑定(bind)事件后通过重置 img.src 再次触发它。以下适用于 IE8、FF 和 Chrome。
$('img').error(function(){
alert('hi');
$(this).hide();
});
$('img').each(function() { this.src = this.src; });
jsFiddle 在这里:http://jsfiddle.net/7cnQN/
关于javascript - 想要在 src 位置找不到图像时隐藏图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4317312/