Home > Web Front-end > JS Tutorial > JS optimizes the img tag and uses onerror to display the default image_javascript skills

JS optimizes the img tag and uses onerror to display the default image_javascript skills

WBOY
Release: 2016-05-16 16:51:24
Original
1035 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 the embarrassment of image loading failure!

js code

Copy code The code is as follows:

//Image loading Handling when errors occur
function errorImg(img) {
img.src = "http://static.xuexiba.com/uploadfile//UserInfo/Avatar/201403/1303992393385832875324.jpg";
img. onerror = null;
}

html code
Copy code The code is as follows:



Page effect
JS optimizes the img tag and uses onerror to display the default image_javascript skills
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