<img src="images/logo.png" onerror="this.src='images/errorLogo.png';">复制代码
2,原生js方法:
document.getElementById('#img').onerror = function(){
this.src = "images/errorLogo.png";
}复制代码
3,jquery封装后的error方法:
$('img').error(function(){
$(this).attr('src',"images/errorLogo.png");
})复制代码
$("img").one("error", function(){
$(this).attr("src", "images/default.png");
});复制代码
function noImg(){
var img=event.srcElement;
img.src="images/errorLogo.png";
img.onerror=null;
}
<img src="images/logo.png" onerror="noImg();">复制代码
background-image:url("images/logo.png"),url("images/errorLogo.png");复制代码
background-repeat: no-repeat;
background-size: cover;复制代码
这种方式的问题:两张图片都加载成功的状况下,第一张图片如有部分透明,会看到第二张图片。css
详情能够查看多背景图像的使用方法:html