从attachEvent获取事件的调用者 [英] Get the caller of the event from attachEvent
本文介绍了从attachEvent获取事件的调用者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试做一些简单的事情:我有一堆正在通过JS加载的图像。
我将一个事件监听器附加到load事件,并且在加载Image之后,在监听器函数中我想从中获取调用Image和检索属性。
这是我的代码,简化:
I am trying to do something simple: I have a bunch of Images which are being load through JS. I attach an event listener to the load event, and after the Image is being loaded, in the listener function I would like to get the calling Image and retrieve properties from it. Here is my code, simplified:
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...
}
推荐答案
试试这个:
var caller = window.event ? window.event.srcElement : e.target;
如果我没记错,当你使用事件
对象。
If I remember rightly IE doesn't pass the event object as a parameter when you've used attachEvent()
, but it has a global event
object.
这篇关于从attachEvent获取事件的调用者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文