jQuery/JavaScript to replace broken images

后端 未结 30 2506
我寻月下人不归
我寻月下人不归 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: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

提交回复
热议问题