<分区>
Possible Duplicate:
How to silently hide “Image not found” icon when src source image is not found
有什么方法可以禁用未找到图像的默认图标吗?如果 src 给出 404,Chrome 会显示小图标。 Firefox 不显示任何内容,我希望在 Chrome 中显示相同内容。 有什么解决办法吗?
标签 html css google-chrome
<分区>
Possible Duplicate:
How to silently hide “Image not found” icon when src source image is not found
有什么方法可以禁用未找到图像的默认图标吗?如果 src 给出 404,Chrome 会显示小图标。 Firefox 不显示任何内容,我希望在 Chrome 中显示相同内容。 有什么解决办法吗?
最佳答案
<img onerror='this.style.display = "none"'>
或
您可以使用 JavaScript 中的 onerror
事件在图片加载失败时采取行动:
var img = document.getElementById("myImg");
img.onerror = function () {
this.style.display = "none";
}
在 jQuery 中:
$("#myImg").error(function () {
$(this).hide();
});
或者对于所有图片:
$("img").error(function () {
$(this).hide();
// or $(this).css({visibility:"hidden"});
});
如果隐藏图像可能会改变布局,您应该使用 visibility: hidden
而不是 .hide()
。 Web 上的许多站点都使用默认的“无图像”图像,当指定的图像位置不可用时,将 src
属性指向该图像。
关于html - Chrome - 找不到图像图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12304213/