When the image does not exist, display a default image

After analysis, it is found that there is code similar to the following in the webpage:

<img src="pic.gif" onerror="javascript:this.src='/noPic.gif';" alt="pic" />
Analysis: Pay special attention to onerror. When the picture does not exist, onerror will be triggered, and a NoPic.gif picture is specified for img in onerror. That is to say, if the picture exists, pic.gif will be displayed, and if the picture does not exist, noPic.gif will be displayed. But here comes the problem, if noPic.gif does not exist, it will continue to trigger onerror, resulting in a loop, so an error occurs.

Description: If the image exists, but the network is not smooth, onerror may also be triggered.

Solution:

The first: ::. remove the onerror code; or change the onerror code to other; or ensure that the picture in onerror is small enough and exists.

The second:

<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; the control does not keep beating

}

//–>

</script>

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

 

[1]. [Code] The image corresponding to the img tag src does not exist, and a default image is displayed 

  
< img src = "abc.JPG" onerror = "this.src='default.JPG'" />

[2].

<script type= "text/javascript" >
   t = document.getElementsByClassName( "defaultImg" );
   for (i = 0; i < t.length; i++){
       t.item(i).onerror = function (){
         this .src = "test.gif"
       }
     }
</script>
//采用 className 是为了更好的适应页面,不是每个img我们都要这样做的,甚至不同地方的img
//我们要显示不同的默认图片。(采用img同样做法)。
//采用id则是犯了错误,id 导致只会拿到第一个id相符的。
//此代码经过测试在 非IE 的browser上正常工作。
 
//为了兼容IE个废物,请使用以下代码:
 
<script type= "text/javascript" >
   t = document.getElementsByTagName( "img" );
   for (i = 0; i < t.length; i++){
       t.item(i).onerror = function (){
         if ( this .id == "defaultImg" ){
             this .src = "test.gif" ;
             this .onerror = null ;
           }
       }
     }
</script>
   

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326616099&siteId=291194637