Home > Web Front-end > JS Tutorial > body text

JS optimizes the img tag and uses onerror to display the default image

高洛峰
Release: 2017-02-04 13:15:43
Original
1245 people have browsed it

There are many reasons why website images are not displayed, such as network problems, file itself problems, file URL problems, etc. When the image fails to load, the onerror event will be triggered. We can use this to effectively avoid image loading failure. Awkward!

js code

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

html code

<img     style="max-width:90%"  style="max-width:90%" src="1.jpg" onerror="errorImg(this)" / alt="JS optimizes the img tag and uses onerror to display the default image" >
Copy after login

Page effect

JS optimizes the img tag and uses onerror to display the default image

More JS optimizes the use of img tags Onerror displays the default image. For related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template