我可以使用Javascript或jQuery检查之前是否已加载图像吗? [英] Can I check whether an image has already been loaded earlier using Javascript or jQuery?

查看:92
本文介绍了我可以使用Javascript或jQuery检查之前是否已加载图像吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑将以下图像附加到文档中:

Consider the following image appended to the document :

<img id="someimage" src="http://www.someimage.jpg">

如果在调用这些函数时图像已经加载,则以下任何一项都不起作用:

None of the following will work if the image has already been loaded by the time these functions are called :

document.getElementById('someimage').onload = function ()
{
  console.log('image just loaded');
};

nor

$("#someimage").load(function ()
{
  console.log('image just loaded');
});

有没有办法解决这个问题?即一个不会等待图像加载但会检查图像是否已经加载的函数?

Is there a way around that ? i.e. a function that will not wait for the image to load but will check whether the image has already been loaded earlier?

谢谢

推荐答案

检查DOM节点的完整属性:

Check the complete property of DOM node:

document.getElementById('someimage').complete;

BTW,这是你应该如何处理的:

BTW, this is how you should handle it:

$("#someimage").one('load', function ()
{
  console.log('image just loaded');
}).each(function(){
    if(this.complete) $(this).trigger('load');
});

这篇关于我可以使用Javascript或jQuery检查之前是否已加载图像吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆