为什么即使没有src标记,Image'complete'属性也总是返回true? [英] Why Image 'complete' property always return true even if there is no src tag?

查看:78
本文介绍了为什么即使没有src标记,Image'complete'属性也总是返回true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是写

document.createElement("img").complete;//To check whether image is loaded or not

在Firefox中,它返回true.在IE中返回false

In Firefox,it returns true. In IE,it return false

OR

在html页面中,只需创建一个图像即可:

In a html page just create one image as:

 <!-- IMG tag with no SRC attribute. -->
<img id="noSrcImg" />

然后在js中检查完整的属性值:

and In js check the complete property value :

var img = document.getElementById("noSrcImg");
img.complete

对于FF为true,对于IE为false

true for FF and false for IE

有人可以解释为什么这种不一致的行为吗?

Can any one explain why this inconsistent behavior?

还有其他更好的方法来检查图像是否已在DOM中加载吗?

Is there any other better way to check whether image is loaded or not in DOM?

我也尝试使用readyState属性,但它不适用于IE11.

i tried with readyState attribute as well but its not available for IE11.

推荐答案

请尝试加载

还要确保在定义src之前已定义事件处理程序-在真正快速的网络上,如果未先定义src,则src可能会在事件处理程序之前加载

Also make sure the event handlers are defined BEFORE you assign src - on really fast networks, the src may load before the event handler if not defined first

var im = document.createElement("img");
im.onload=function() { alert(this.src+' loaded')} // assign before src
im.onerror=function() { alert(this.src+' failed')} // if necessary
im.src="someimage.jpg";

这篇关于为什么即使没有src标记,Image'complete'属性也总是返回true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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