How to access Event.target in IE9?

前端 未结 6 649
耶瑟儿~
耶瑟儿~ 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 02:40

    If you want to use event.target in IE9, you'll need to use addEventListener()-method to assign eventhandler to the element.

    <div id="day" class="day"></div>
    
    document.getElementById('day').addEventListener('click',divClick,false);
    
    function divClick(e){
       alert(e.target);
       divCell=this;
       :
    }
    

    In divClick() you can refer day simply using keyword this. Argument e contains a reference to the event-object itself.

    BTW, in MSDN you can find maybe more suitable IE-documentation for Web development instead of Windows development.

    0 讨论(0)
  • 2021-02-08 02:40

    event.target ?

    Do you need to check on it and assign that to a variable and use that instead ..

      var target = event.target ? event.target : event.srcElement;
    

    might be missing the point...

    0 讨论(0)
  • 2021-02-08 02:40

    use:

    var target = (event.target !== undefined)? event.target.name : event.srcElement.tagName;
    

    then:

    if ( target === 'A' || target === '...') {
        ...
    }
    

    OR: simple use:

    var target = event.srcElement.tagName;
    

    tested on Chrome 35 :)

    0 讨论(0)
  • 2021-02-08 02:43

    The easy way to archive this is by using:

    var target = event.target || event.srcElement;
    
    • Avoid to use a ternary operator.

    The way this works is because:

    1. Test if event.target is a truthy which if does not exist in that browser will evaluate to false.
    2. If evaluates to false then it will move to the next operator which in this case is event.srcElement.

    And with this you will end up with the first value that is present on the current browser where the code is executed.

    0 讨论(0)
  • 2021-02-08 02:49

    Here goes for my investigation (tested in IE9, IE10 & Edge browser modes on IE11, IE8 unfortunately breaks jsFiddle). In IE9 mode (IE11) event.target was available as a local variable to the function, don't know if it differs from the real IE9.

    You cannot access event in any browser with an inline function. The problem is that when you do

    <element onclick="someFunc(this)">
    

    and you pass this as parameter, this === event.target (or srcElement), namely a [HTML Object], not an [Event Object].

    So in practice that means that this:

    <div id="foo" onclick="alert(this)"></div>
    

    is the same as:

    // note that onclick perfectly works, you don't necessarily need addEventListener
    document.getElementById('foo').onclick = function(e) { alert(e.target) } //or
    document.getElementById('foo').addEventListener('click', function(e) { alert(e.target) }, false);
    

    So you access event target either directly inline as the parameter, either through javascript as the object parameter's target || srcElement property. You can test the results for yourself here: http://jsfiddle.net/kevinvanlierde/tg6FP/2/

    Note: In case you attach inline, the position of your scripts is crucial (right before closing body tag)
    Note: In case you attach inline, given that event.target is the 'root' of the object passed, you cannot access other event properties, like event.type.
    Note: Be careful with IE Developer mode. I've known it to be deceiving (eg, not correctly displaying DOM content in the element tree until you click 'Edit as HTML')

    0 讨论(0)
  • 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.

    <div id=xyz onclick="divClick(this);">
    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)
    
    0 讨论(0)
提交回复
热议问题