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

前端 未结 15 1321
挽巷
挽巷 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:18

    Another option is to trigger the onload and/or onerror events by creating an in memory image element and setting its src attribute to the original src attribute of the original image. Here's an example of what I mean:

    $("")
        .on('load', function() { console.log("image loaded correctly"); })
        .on('error', function() { console.log("error loading image"); })
        .attr("src", $(originalImage).attr("src"))
    ;
    

    Hope this helps!

提交回复
热议问题