FireFox和Safari兼容event.path

在项目开发中遇到需要获取触发事件元素冒泡过程的所有元素,在Chrome中可以通过event.path获取。

element.onClick(event) {
  const ev = window.event || event;
  const path = ev.path;
}

《FireFox和Safari兼容event.path》

该属性在Chrome和Opera浏览器下没问题,但是在Firefox和Safari中发现event并没有path属性。 进过查找资料发现,在浏览器新的标准里定义的composedPath可以获取

element.onClick(event) {
  const ev = window.event || event;
  const path = event.path || (event.composedPath && event.composedPath());
  console.log(path)  //[button#btn, div, body, html, document, Window]
}
    原文作者:LDP_Rou
    原文地址: https://segmentfault.com/a/1190000019515284
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞