码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-06-02 11:33:41      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:doc   one   hid   tar   存在   事件   ima   ref   isp   

HTML:

<img src="http://www.mazey.net/images/upload/image/20170518/1495122198180663.gif" id="img1" onerror="hideImg1();">
<!--下面这个图加载失败但不会出现加载失败的样式-->
<img src="http://www.mazey.net/images/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;}使其消失。

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

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

标签:doc   one   hid   tar   存在   事件   ima   ref   isp   

原文地址:http://www.cnblogs.com/mazey/p/6931828.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!