JavaScript-onerror事件:图片加载失败后不显示

HTML:

<img src="http://www.mazey.net/img/upload/image/20170518/1495122198180663.gif" id="img1" onerror="hideImg1();">
<!--下面这个图加载失败但不会出现加载失败的样式-->
<img src="http://www.mazey.net/img/upload/image/20170518/1495120456257031error.png" id="img2" onerror="hideImg2();">

JavaScript:

//加载失败隐藏图片
function hideImg1(){
	document.getElementById("img1").style.display="none";
}
function hideImg2(){
	document.getElementById("img2").style.display="none";
}

说明:

JavaScript-onerror事件:当图片不存在时或者因为网络原因加载失败,将触发onerror事件。

本例触发onerror事件后将图片样式设为{display:none;}使其加载失败后在网页中消失。

1 条评论

  1. reviewer
    BestJamal
    2019年8月4日

    I see you don't monetize mazey.net, don't waste your traffic,
    you can earn additional cash every month with new
    monetization method. This is the best adsense alternative for any type of website (they approve all
    websites), for more info simply search in gooogle: murgrabia's tools

    回复

BestJamal进行回复 取消回复

您的电子邮箱地址不会被公开。