Check if an image is loaded (no errors) with jQuery

前端 未结 15 1561
挽巷
挽巷 2020-11-21 20:45

I\'m using JavaScript with the jQuery library to manipulate image thumbnails contained in a unordered list. When the image is loaded it does one thing, when an error occurs

15条回答
  •  醉梦人生
    2020-11-21 21:10

    I tried many different ways and this way is the only one worked for me

    //check all images on the page
    $('img').each(function(){
        var img = new Image();
        img.onload = function() {
            console.log($(this).attr('src') + ' - done!');
        }
        img.src = $(this).attr('src');
    });
    

    You could also add a callback function triggered once all images are loaded in the DOM and ready. This applies for dynamically added images too. http://jsfiddle.net/kalmarsh80/nrAPk/

提交回复
热议问题