jQuery/JavaScript to replace broken images

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

    I use the built in error handler:

    $("img").error(function () {
        $(this).unbind("error").attr("src", "broken.gif");
    });
    

    Edit: The error() method is deprecated in jquery 1.8 and higher. Instead, you should use .on("error") instead:

    $("img").on("error", function () {
        $(this).attr("src", "broken.gif");
    });
    

提交回复
热议问题