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
event.toElement in IE8 and Firefox?
提问by captainclam
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 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
In Firefox it is event.relatedTarget
https://developer.mozilla.org/en/DOM:event.relatedTarget#1003983
在 Firefox 中是event.relatedTarget
https://developer.mozilla.org/en/DOM:event.relatedTarget#1003983
回答by simomo
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.
我在使用 Jay 的回答时遇到了一个问题,event.target
在 Firefox 上指向event.toElement
chrome上's target的父元素。
在查看事件 obj 后,我发现event.originalEvent.target
它在 Firefox 和 chrome 上都运行良好。
回答by gpasse
Actually event.currentTarget
should 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 target
to 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 currentTarget
attribute.
触发事件的元素存储在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 e
is the event
哪里e
是event
回答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