<img src="img/789.png" onerror="javascript:this.src='img/123.png';" alt="pic" />
注意:当img/789.png图片不存在时,将触发 onerror,而 onerror 中又为 img 指定一个 img/123.png图片。也就是说图片存在则显示 img/789.png,图片不存在将显示img/123.png。但问题来了,如果 img/123.png也不存在,则继续触发 onerror,导致循环,故出现错误。
说明:如果图片存在,但网络很不通畅,也可能触发 onerror。
解决方法:
第一种::.去掉 onerror 代码;或者更改 onerror 代码为其它;或者确保 onerror 中的图片足够小,并且存在。
第二种:
(1):
<img src="img/123.png” onerror="nofind();" /> <script type="text/javascript"> function nofind(){ var img=event.srcElement; img.src="http://www.cnblogs.com/sys/common/image/fileoperation/icon/default.gif"; img.onerror=null; 控制不要一直跳动 } </script>
(2):
<img src="?" id="imgId" onerror="this.onerror=null;this.src='images/noavatar.png'"> <script> document.getElementById("imgId").src = window.location.origin+"/api/uc/download_img/"+userData.userId; </script>