Internet Explorer and JavaScript event currentTarget

后端 未结 13 2246
执笔经年
执笔经年 2020-11-30 05:36

Is there a way to take the current target of an event with IE 7 or 8?

With other browser (firefox, opera, chrome etc.) we can use event.currentTarget or

13条回答
  •  情书的邮戳
    2020-11-30 06:02

    I'd like to give a more simple answer, the meat of this is the same as the meat in anas' and user1515360's answers:

    if (browserDoesNotUnderstandCurrentTarget) {
            someElement.onclick = function (e) {
                    if (!e) { var e = window.event; }
                    e.currentTarget = someElement;
                    yourCallback(e);
            }
    }
    else {
            someElement.onclick = yourCallback;
    }
    

    Substitute onclick for whatever event you wish of course.

    This makes e.currentTarget available on browsers that do not support currentTarget.

提交回复
热议问题