[英]What is the difference between e.target.parentNode and e.path[1]
I was wondering what the difference between the e.target.parentNode
and e.path[1]
is? 我想知道
e.target.parentNode
和e.path[1]
之间的区别是什么? And if there is, what is better & why? 如果有,哪个更好,为什么呢?
Here is how the sturcture looks like: 结构如下所示:
Here ist the console log: 在此显示控制台日志:
console.log("button clicked!!!",e.target.parentNode, e.path[1], e.path[2])
Here is the click console.log(e)
这是单击
console.log(e)
NB: click event is on the button element 注意:单击事件在按钮元素上
Thank you 谢谢
Experimentally, on the one browser I've found that supports it, e.path[1]
and e.target.parentNode
refer to the same element. 在实验上,我发现在一个支持它的浏览器上,
e.path[1]
和e.target.parentNode
指向同一元素。 Eg, there's no difference. 例如,没有区别。
But note that Event#path
is not well-supported (whereas Event#target
and Node#parentNode
are), and as far as I can tell, it's not (yet?) standardized: It doesn't appear in the DOM4 Event
interface , for instance. 但是请注意,
Event#path
的支持不充分(而Event#target
和Node#parentNode
则受支持),据我所知,它尚未标准化(尚未?):它没有出现在DOM4 Event
接口中 ,例如。 It's also not in the WHAT-WG Event
interface , but that spec does mention a method , composedPath
, that appears to return the same sort of information. 它也不在WHAT-WG
Event
接口中 ,但是该规范确实提到了一个方法名为composedPath
,它似乎返回了相同类型的信息。
Chrome 51 seems to support Event#path
; Chrome 51似乎支持
Event#path
; Firefox 47 does not. Firefox 47没有。 Neither supports
composedPath
. 都不支持
composedPath
。
You can see if your current browser supports it here: 您可以在此处查看当前的浏览器是否支持它:
document.getElementById("outer").addEventListener("click", function(e) { var hasPath = !!e.path; var hasComposePath = !!e.composedPath; console.log("Has `path`? " + (hasPath ? "Yes" : "No")); console.log("Has `composedPath()`? " + (hasComposePath ? "Yes" : "No")); if (hasPath) { console.log("e.path[1] === e.target.parentNode? " + (e.path[1] === e.target.parentNode)); } });
<div id="outer"> <div id="inner"> Click me </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.