JS optimizes the img tag using onerror to display the default image

  • 2020-03-30 02:42:00
  • OfStack

There are many reasons for the website image not to display, such as network problems, problems with the file itself, file URL problems, etc., and when the image loading failure will trigger the event of onerror, we use this, can effectively avoid the embarrassing image loading failure!

Js code
//Image loading error handling
function errorImg(img) { 
img.src = "http://static.xuexiba.com/uploadfile//UserInfo/Avatar/201403/1303992393385832875324.jpg"; 
img.onerror = null; 

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

Effect of the page
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201404/201404241614072.gif? 2014324161426 ">

Related articles: