Detect a img src change

前端 未结 7 1509
被撕碎了的回忆
被撕碎了的回忆 2020-12-16 16:20

I\'am trying to detect if the source of a image is changed.

In my case the src is changed from jquery, and i have no right\'s to change the jquery file. So im trying

7条回答
  •  执念已碎
    2020-12-16 17:02

    I think there is no event for that, you can create your own 'event':

    var divimg = document.getElementById("img_div"),
        prevSrc;
    setInterval(function() {
        if (divimg.src != prevSrc) {
            prevSrc = divimg.src;
            onSrcChange();
        }
    }, 1000); // 1000ms = 1s
    
    function onSrcChange() {
        // do something
    }
    

提交回复
热议问题