为什么我只在IE9中获取图像完整属性的错误? [英] Why am I getting a false only in IE9 for image complete property?

查看:104
本文介绍了为什么我只在IE9中获取图像完整属性的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到这种模式使用了很多,但似乎IE9不喜欢它。以下是我的函数的大致概念:

I've seen this pattern used quite a bit, but it seems IE9 doesn't like it. Here is a rough idea of what my function does:

function(path){
    $("<img/>",{"src":path}).one("load",function(event,alreadyLoaded) {
        if(!alreadyLoaded) { 
            myObject.loadedImages = myObject.loadedImages || [];
            myObject.loadedImages.push(this);
        }
    // Other code here...                   
    }).each(function() {
        if(this.complete) {
            $(this).trigger("load",true);
        }
    });
}

我意识到这可能是重复的,但我看到的建议不是工作:
(例如this.readyState //返回未初始化)

I realize this might be a duplicate, but the suggestions I've seen aren't working: (e.g. this.readyState // returns uninitialized)

如果有人可以请我指出正确的方向,那就太棒了。谢谢。

If someone could please point me in the right direction, that would be great. Thanks.

推荐答案

.one()适用于每个元素,而不是每个元素 src 属性或图像文件。因此,如果您创建两个单独的图像元素并在它们上面调用 .one(load,...),则会为两个图像触发加载事件,甚至虽然它们共享一个源并且图像文件本身被缓存。

.one() applies per element, not per src attribute or image file. So, if you create two separate image elements and call .one("load", ...) on both of them, the load event will fire for both images, even though they share a source and the image file itself is cached.

为了防止数组中出现重复,请改为使用哈希:

To prevent duplicates in your array, use a hash instead:

function addImage (path) { 
    $("<img/>").load(function (e) { 
        myObject.loadedImages = myObject.loadedImages || {};
        if(!this.src in myObject.loadedImages) {  
            myObject.loadedImages[this.src] = this; 
        }
    }).attr({ "src": path });
} 

这篇关于为什么我只在IE9中获取图像完整属性的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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