我正在尝试做一些简单的事情:我有一堆正在通过 JS 加载的图像。我将一个事件侦听器附加到加载事件,并且在加载图像之后,在侦听器函数中,我想获取调用图像并从中检索属性。这是我的代码,简化:
function loadImages() {
for (var i = 0; i < arrDownloadQueueBasic.length; i++) {
var path = arrDownloadQueueBasic[i].path;
var img = new Image();
img.type = arrDownloadQueueBasic[i].type;
img.attachEvent(img, 'load', setBasicElement);
img.src = path;
}
}
function setBasicElement(e) {
var caller = e.target || e.srcElement;
alert(caller); // THIS DOESNT WORK - RETURN NULL
alert(caller.type) // OF COURSE THIS DOESNT WORK AS WELL...
}