H5W3
当前位置:H5W3 > JavaScript > 正文

【JS】FireFox和Safari兼容event.path

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

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

【JS】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]
}

https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…
https://xueqiu.com/8149907930…

本文地址:H5W3 » 【JS】FireFox和Safari兼容event.path

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址