美文网首页
FireFox和Safari兼容event.path

FireFox和Safari兼容event.path

作者: Ruo_LDP | 来源:发表于2018-08-21 10:42 被阅读0次

在项目开发中遇到需要获取触发事件元素冒泡过程的所有元素,在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]
}

相关文章

网友评论

      本文标题:FireFox和Safari兼容event.path

      本文链接:https://www.haomeiwen.com/subject/ssediftx.html