Why am I getting a false only in IE9 for image complete property?

做~自己de王妃 提交于 2019-12-12 04:25:21

问题


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);
        }
    });
}

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.


回答1:


.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 });
} 


来源:https://stackoverflow.com/questions/8338770/why-am-i-getting-a-false-only-in-ie9-for-image-complete-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!