Chrome - 找不到图片图标

tun*_*rob 3 html css google-chrome

可能重复:如果
未找到src源图像,如何以静默方式隐藏"未找到图像"图标

有没有办法,禁用未找到图像的默认图标?如果src给出404,则Chrome会显示小图标.Firefox没有显示任何内容,我想在Chrome中使用相同内容.任何解决方案?

Far*_*her 7

<img onerror='this.style.display = "none"'>
Run Code Online (Sandbox Code Playgroud)

要么

您可以使用onerrorJavaScript中的事件在图像加载失败时执行操作:

var img = document.getElementById("myImg");
img.onerror = function () { 
    this.style.display = "none";
}
Run Code Online (Sandbox Code Playgroud)

在jQuery中:

$("#myImg").error(function () { 
    $(this).hide(); 
});
Run Code Online (Sandbox Code Playgroud)

或者对于所有图像:

$("img").error(function () { 
    $(this).hide();
    // or $(this).css({visibility:"hidden"}); 
});
Run Code Online (Sandbox Code Playgroud)

您应该使用visibility: hidden而不是.hide()隐藏图像可能会更改布局.Web上的许多站点使用默认的"无图像"图像,src当指定的图像位置不可用时将属性指向该图像.