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
WBOYOriginal
2016-05-16 16:51:241038browse

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
Statement:
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