且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查图像是否被加载(没有错误)在JavaScript

更新时间:2023-11-26 11:42:16

另一个选项是触发 onload 和/或 onerror 事件创建内存图像元素并将其 src 属性设置为原始图像的原始 src 属性。以下是我的意思:

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:

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

希望这有帮助!