event.toElement in IE8 and Firefox?

后端 未结 6 1335
梦如初夏
梦如初夏 2020-12-01 13:49

I have noticed that in Chrome and IE9, for onmouseout events there is an event.toElement property (so you can determine which element the mouse is

相关标签:
6条回答
  • 2020-12-01 14:01

    code easy to follow..

    enter code here
    if(typeof evt.toElement !== "undefined")
    {
            evt.toElement.classList.toggle('done');
    }
    else if(typeof evt.relatedTarget !== "undefined")
    {
        if(evt.relatedTarget !== null)
        {
            evt.relatedTarget.classList.toggle('done');
        }
        else if(typeof evt.currentTarget !== "undefined")
        {
            evt.currentTarget.classList.toggle('done');
        }
        else
        {
        console.log("s_f_li_clickexception...");    
        } //endif
    } //endif
    
    0 讨论(0)
  • 2020-12-01 14:03

    Instead of event.toElement you should use this:

    event.target
    
    0 讨论(0)
  • 2020-12-01 14:11

    In Firefox it is event.relatedTarget https://developer.mozilla.org/en/DOM:event.relatedTarget#1003983

    0 讨论(0)
  • 2020-12-01 14:15

    I met a issue when I use Jay's answer, event.target on firefox points to the parent element of event.toElement's target on chrome.
    After looking into the event obj, I find event.originalEvent.target, it works good on both firefox and chrome.

    0 讨论(0)
  • 2020-12-01 14:21

    As of 2014, IE11 doesn't support toElement, I looked through the event object and found target to have the same data as toElement.

    That is to say, if you click on a child element inside an element that this event triggered on, the child element will be the 'target' and stored in this attribute.

    The element the event fired from is stored in the currentTarget attribute.

    Note, I've only tested this for ie 11 so older versions may not support this.

    So to support firefox ie and chrome (and possibly others, a polyfill would be necessary, something like:

    var target = e.toElement || e.relatedTarget || e.target || function () { throw "Failed to attach an event target!"; }
    

    Where e is the event

    0 讨论(0)
  • 2020-12-01 14:25

    Actually event.currentTarget should work in Chrome, Firefox and IE

    0 讨论(0)
提交回复
热议问题