Javascript IE8 和 Firefox 中的 event.toElement?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8600174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:40:24  来源:igfitidea点击:

event.toElement in IE8 and Firefox?

javascriptdomcross-browserdom-events

提问by captainclam

I have noticed that in Chrome and IE9, for onmouseoutevents there is an event.toElementproperty (so you can determine which element the mouse is now pointing at).

我注意到在 Chrome 和 IE9 中,对于onmouseout事件有一个event.toElement属性(因此您可以确定鼠标现在指向哪个元素)。

I can not find a comparable property in Firefox.

我在 Firefox 中找不到类似的属性。

Unfortunately I can not use jQuery to handle these events, I have to use native js.

不幸的是我不能使用 jQuery 来处理这些事件,我必须使用原生 js。

Any advice would be appreciated.

任何意见,将不胜感激。

采纳答案by Azam Alvi

Instead of event.toElement you should use this:

而不是 event.toElement 你应该使用这个:

event.target

回答by captainclam

回答by simomo

I met a issue when I use Jay's answer, event.targeton 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.

我在使用 Jay 的回答时遇到了一个问题,event.target在 Firefox 上指向event.toElementchrome上's target的父元素。
在查看事件 obj 后,我发现event.originalEvent.target它在 Firefox 和 chrome 上都运行良好。

回答by gpasse

Actually event.currentTargetshould work in Chrome, Firefox and IE

实际上event.currentTarget应该适用于 Chrome、Firefox 和 IE

回答by Jay

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

截至 2014 年,IE11 不支持toElement,我查看了事件对象,发现target与 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 currentTargetattribute.

触发事件的元素存储在currentTarget属性中。

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

请注意,我仅针对 ie 11 对此进行了测试,因此旧版本可能不支持此功能。

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

因此,为了支持 firefox ie 和 chrome(可能还有其他的,polyfill 是必要的,例如:

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

Where eis the event

哪里eevent

回答by joseAndresGomezTovar

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