【JS】FireFox和Safari兼容event.path
在项目开发中遇到需要获取触发事件元素冒泡过程的所有元素,在Chrome中可以通过event.path获取。
element.onClick(event) {const ev = window.event || event;
const path = ev.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...
以上是 【JS】FireFox和Safari兼容event.path 的全部内容, 来源链接: utcz.com/a/96278.html