js determine whether there is a picture

Note: This reference to other people's solutions, and it has been optimized.

Principle: Determine the picture by judging whether or not there is image size.

code show as below:

. 1 $ ( 'IMG') each (. Function () {
 2      var ImgObj = new new Image ();
 . 3  
. 4      ImgObj.src = the src;
 . 5  
. 6      // conditions existing image 
. 7      var exist ImgObj.fileSize => 0 || (ImgObj.width> 0 && ImgObj.height> 0 );
 . 8  
. 9      // when the file does not exist, it is replaced with a default image 
10      iF (exist!) the this .src = 'default.jpg' ;
 . 11 });

 

Guess you like

Origin www.cnblogs.com/blackMagic/p/11578599.html