jQuery/JavaScript to replace broken images

后端 未结 30 2557
我寻月下人不归
我寻月下人不归 2020-11-21 05:50

I have a web page that includes a bunch of images. Sometimes the image isn\'t available, so a broken image is displayed in the client\'s browser.

How do I use jQuery

相关标签:
30条回答
  • 2020-11-21 06:09

    jQuery 1.8

    // If missing.png is missing, it is replaced by replacement.png
    $( "img" )
      .error(function() {
        $( this ).attr( "src", "replacement.png" );
      })
      .attr( "src", "missing.png" );
    

    jQuery 3

    // If missing.png is missing, it is replaced by replacement.png
    $( "img" )
      .on("error", function() {
        $( this ).attr( "src", "replacement.png" );
      })
      .attr( "src", "missing.png" );
    

    reference

    0 讨论(0)
  • 2020-11-21 06:10
    $(window).bind('load', function() {
      $('img').each(function() {
        if( (typeof this.naturalWidth != "undefined" && this.naturalWidth == 0) 
        ||  this.readyState == 'uninitialized'                                  ) {
            $(this).attr('src', 'missing.jpg');
        }
      });
    });
    

    Source: http://www.developria.com/2009/03/jquery-quickie---broken-images.html

    0 讨论(0)
  • 2020-11-21 06:11

    Here is a standalone solution:

    $(window).load(function() {
      $('img').each(function() {
        if ( !this.complete
        ||   typeof this.naturalWidth == "undefined"
        ||   this.naturalWidth == 0                  ) {
          // image was broken, replace with your new image
          this.src = 'http://www.tranism.com/weblog/images/broken_ipod.gif';
        }
      });
    });
    
    0 讨论(0)
  • 2020-11-21 06:13

    This is JavaScript, should be cross browser compatible, and delivers without the ugly markup onerror="":

    var sPathToDefaultImg = 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
        validateImage = function( domImg ) {
            oImg = new Image();
            oImg.onerror = function() {
                domImg.src = sPathToDefaultImg;
            };
            oImg.src = domImg.src;
        },
        aImg = document.getElementsByTagName( 'IMG' ),
        i = aImg.length;
    
    while ( i-- ) {
        validateImage( aImg[i] );
    }
    

    CODEPEN:

    0 讨论(0)
  • 2020-11-21 06:16

    This has been frustrating me for years. My CSS fix sets a background image on the img. When a dynamic image src doesn't load to the foreground, a placeholder is visible on the img's bg. This works if your images have a default size (e.g. height, min-height, width and/or min-width).

    You'll see the broken image icon but it's an improvement. Tested down to IE9 successfully. iOS Safari and Chrome don't even show a broken icon.

    .dynamicContainer img {
      background: url('/images/placeholder.png');
      background-size: contain;
    }
    

    Add a little animation to give src time to load without a background flicker. Chrome fades in the background smoothly but desktop Safari doesn't.

    .dynamicContainer img {
      background: url('/images/placeholder.png');
      background-size: contain;
      animation: fadein 1s;                     
    }
    
    @keyframes fadein {
      0%   { opacity: 0.0; }
      50%  { opacity: 0.5; }
      100% { opacity: 1.0; }
    }
    
    0 讨论(0)
  • 2020-11-21 06:17

    Handle the onError event for the image to reassign its source using JavaScript:

    function imgError(image) {
        image.onerror = "";
        image.src = "/images/noimage.gif";
        return true;
    }
    
    <img src="image.png" onerror="imgError(this);"/>
    

    Or without a JavaScript function:

    <img src="image.png" onError="this.onerror=null;this.src='/images/noimage.gif';" />
    

    The following compatibility table lists the browsers that support the error facility:

    http://www.quirksmode.org/dom/events/error.html

    0 讨论(0)
提交回复
热议问题