`

js注意img图片的onerror事件的分析

阅读更多

我们经常在<img>中加个onerror标签,如果图片加载失败把src指定到指定的图片,但是有没有想过如果这个指定的图片也加载失败会怎样,答案是会无限循环,为防止这种错误,可以这样做:

<script type="text/javascript"> 
<!– 
function nofind(){ 
var img=event.srcElement; 
img.src="../../../sys/common/image/fileoperation/icon/default.gif"; 
img.onerror=null; 控制不要一直跳动 

//–> 
</script> 
<td align="center"><img src="../../../sys/common/image/fileoperation/icon/${file.suffix }.gif" onerror="nofind();" />${file.name }</td> 

这样如果加载错误只会执行一次onerror指令,不会出现死循环了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics