且构网

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

jQuery或Javascript检查图像是否已加载

更新时间:2023-11-26 11:07:34

我通常这样做:

var image = new Image();
image.onload = function () {
   console.info("Image loaded !");
   //do something...
}
image.onerror = function () {
   console.error("Cannot load image");
   //do something else...
}
image.src = "/images/blah/foo.jpg";

请记住加载是异步的,所以你必须继续 onload onerror 事件。

Remember that the loading is asynchronous so you have to continue the script inside the onload and onerror events.