Maison  >  Article  >  interface Web  >  JS对img标签进行优化使用onerror显示默认图像

JS对img标签进行优化使用onerror显示默认图像

高洛峰
高洛峰original
2017-02-04 13:15:431199parcourir

对于网站图像的不显示有很多原因,网络问题,文件本身问题,文件URL问题等,而当图像加载失败时会触发onerror这个事件,我们利用这点,可以有效的避免图像加载失败的尴尬! 

js代码 

//图像加载出错时的处理 
function errorImg(img) { 
img.src = "http://static.xuexiba.com/uploadfile//UserInfo/Avatar/201403/1303992393385832875324.jpg"; 
img.onerror = null; 
}

html代码 

<img width="32" height="32" src="1.jpg" onerror="errorImg(this)" />

页面效果

JS对img标签进行优化使用onerror显示默认图像

更多JS对img标签进行优化使用onerror显示默认图像相关文章请关注PHP中文网!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn