栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Firefox中运行的event.path是未定义的

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Firefox中运行的event.path是未定义的

对象的

path
属性
Event
是非标准的。标准等效项是
composedPath
,这是一种方法。但这是新的。

因此,您可能想要尝试回退到该位置,例如:

var path = event.path || (event.composedPath && event.composedPath());if (path) {    // You got some path information} else {    // This browser doesn't supply path information}

显然,如果浏览器不提供路径信息,它将不会为您提供路径信息,但是它同时支持旧方法和新的标准方法,因此将尽其所能地跨浏览器。

例:

document.getElementById("target").addEventListener("click", function(e) {  // Just for demonstration purposes  if (e.path) {    if (e.composedPath) {      console.log("Supports `path` and `composedPath`");    } else {      console.log("Supports `path` but not `composedPath`");    }  } else if (e.composedPath) {    console.log("Supports `composedPath` (but not `path`)");  } else {    console.log("Supports neither `path` nor `composedPath`");  }  // Per the above, get the path if we can  var path = e.path || (e.composedPath && e.composedPath());  // Show it if we got it  if (path) {    console.log("Path (" + path.length + ")");    Array.prototype.forEach.call(      path,      function(entry) {        console.log(entry.nodeName);      }    );  }}, false);<div id="target">Click me</div>

在我的测试(2018年5月更新)中,IE11和Edge都不支持

path
composedPath
。Firefox支持
composedPath
。Chrome支持
path
(这是Google的原始想法)和
composedPath

因此,我认为您无法直接在IE11或Edge上获取路径信息。你可以很明显,获得通过的路径

e.target.parentNode
和每个随后
parentNode
,这
通常 是相同的,但当然点的
path
/
composedPath
是,它并不 总是
该事件被触发后,相同的(如果事情修改DOM但在此之前你的处理器接到电话)。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/575374.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号