Internet Explorer and JavaScript event currentTarget

后端 未结 13 2216
执笔经年
执笔经年 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 05:57

    Internet Explorer 6 - 8 do not implement event.currentTarget.

    Quote from Mozilla Developer Network:

    On Internet Explorer 6 through 8, the event model is different. Event listeners are attached with the non-standard element.attachEvent method. In this model, there is no equivalent to event.currentTarget and this is the global object. One solution to emulate the event.currentTarget feature is to wrap your handler in a function calling the handler using Function.prototype.call with the element as a first argument. This way, this will be the expected value.

    If you use jQuery anyway, then jQuery takes care of normalizing the event object and its properties (as stated numerous times in the other answers). Quote from jQuery API docs:

    jQuery’s event system normalizes the event object according to W3C standards.

提交回复
热议问题