如果readyState加载时发生register事件,则在IE9中未触发onload事件

onload event not firing in IE9 if register event when readyState is loading

本文关键字:事件 IE9 onload 则在 readyState register 如果 加载      更新时间:2023-09-26

我试图在HTML中放置多个图像,并在它们全部加载时对它们进行操作。

但加载事件有时并没有在IE9中启动。

我设置了一些控制台调试消息,发现如果在注册onload事件时正在加载映像的readyState,那么onload事件将不会触发。

代码片段如下:

var loaded = function () {
    // increate counter to make sure all images are loaded or not
};
var thumbnails = $('img');
thumbnails.each(function () {
    console.log('this.readyState:'+this.readyState)
    if (this.complete) {
        loaded();
    } else {
        this.onload = loaded;
    }
});

附言:我不想使用window.onload方法,因为我的脚本可能是一个插件,它将插入某人的页面。

尝试重新分配图像源(src)以触发事件,如:

var thumbnails = $('img');
thumbnails.each(function () {
    var img = $(this);
    console.log('this.readyState:'+this.readyState)
    if (this.complete) {
        loaded();
    } else {
        this.onload = loaded;
    }
    img.attr('src', img.attr('src'));
});