How to access Event.target in IE9?

前端 未结 6 650
耶瑟儿~
耶瑟儿~ 2021-02-08 02:12

The HTML DOM object model defines an Event object with a target property.

Looking at MSDN, Microsoft documents a target property. They also document srcElem

6条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-08 03:01

    The problem here was not that you didn’t use addEventListener() (because old-school .onclick-assignment works as well). The problem was that divClick got this (which equals eventobj.target), but you want the entire eventobj.

    In the following two cases you only get eventobj.target under the name of this and you have no access to the arguments passed to the onclick-handler.

    document.getElementById("xyz").onclick = function () { divClick(this); };

    You get the entire eventobj as normal parameter with the following line:

    document.getElementById("xyz").onclick = divClick;
    

    This is how an event is launched on the xyz element in IE9+ and other browsers:

    document.getElementById("xyz").onclick(eventobj)
    

提交回复
热议问题