首先,我使用的是vue.js和node.js
我的火狐有问题。
我使用了event.path[n].id
并且在firefox中我得到了一个错误
未定义的event.path
但它在其他浏览器中也能正常工作。
你知道为什么吗?
最佳答案:
path
对象的Event
属性是非标准的。标准等效物是一种方法。但它是新的。
因此,您可能想尝试回到这个问题上,例如:
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都不支持
composedPath
或path
。firefox支持composedPath
。Chrome同时支持composedPath
(这是Google的原始想法)和path
。所以我认为你不能直接在IE11或EDGE上获得路径信息。很明显,您可以通过
composedPath
和每个后续的e.target.parentNode
获得路径,这通常是相同的,但是,当然parentNode
/path
的要点是它并不总是相同的(如果在事件触发后但在调用处理程序之前修改了DOM)。