jQuery/JavaScript to replace broken images

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

    I found this post while looking at this other SO post. Below is a copy of the answer I gave there.

    I know this is an old thread, but React has become popular and, perhaps, someone using React will come here looking for an answer to the same problem.

    So, if you are using React, you can do something like the below, which was an answer original provided by Ben Alpert of the React team here

    getInitialState: function(event) {
        return {image: "http://example.com/primary_image.jpg"};
    },
    handleError: function(event) {
        this.setState({image: "http://example.com/failover_image.jpg"});
    },
    render: function() {
        return (
            ;
        );
    }
    

提交回复
热议问题