简体   繁体   English

e.target.parentNode和e.path [1]有什么区别

[英]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.parentNodee.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#targetNode#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.

相关问题 从 e.target.parentNode 获取特定 div 后,我们如何访问它的某些属性/属性 - how can we access some property/attribute of particular div after getting it from e.target.parentNode e.target和e.relatedTarget有什么区别? - What is the difference between e.target and e.relatedTarget? 从事件侦听器 e.path 数组中提取元素的 id? - Extracting the id of an element from Event Listener e.path Array? 如何在 addEventListener 跨浏览器中使用 e.path? - How to use e.path in addEventListener cross-browser? enyo.dispatch(e)和target.dispatchEvent(e)Web API的行为差异 - Behavioral difference between enyo.dispatch(e) and target.dispatchEvent(e) web api E4X 中的 elements() 和 children() 方法有什么区别 - What's the difference in E4X between the elements() and children() method 如何使用例如event.target和element.parentNode配置ESLint? - How to configure ESLint for using e.g. event.target and element.parentNode? 为什么来自 e.target.parentNode.children[0].innerText 的值永远不等于来自过滤器 function 的值? - How come value from e.target.parentNode.children[0].innerText never equal to value from filter function? 在编写 promise.catch() 块时,e 和 e.message 有什么区别? - When writing promise .catch() blocks, what's the difference between e and e.message? $(e.target).find 和 template.find(&#39;input&#39;).value 有什么区别 - Whats the difference between $(e.target).find and template.find('input').value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM