Han*_*son 34 javascript firefox node.js vue.js
首先我使用的是Vue.js和Node.js
我有Firefox的问题.
我使用event.path[n].id和Firefox一起出现错误
event.path undefined
但它在其他浏览器中工作正常.
你知道为什么吗?
T.J*_*der 59
对象的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
}
Run Code Online (Sandbox Code Playgroud)
显然,如果浏览器不提供路径信息,它将不会提供路径信息,但它允许旧的方式和新的标准方式,因此将做最好的跨浏览器.
例:
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);Run Code Online (Sandbox Code Playgroud)
<div id="target">Click me</div>Run Code Online (Sandbox Code Playgroud)
在我的测试中(2018年5月更新),IE11和Edge都不支持path或composedPath.Firefox支持composedPath.Chrome支持这两者path(这是谷歌最初的想法)和composedPath.
所以我认为你不能直接在IE11或Edge上获取路径信息.你可以很明显,获得通过的路径e.target.parentNode和每个随后parentNode,这通常是相同的,但当然点path/ composedPath是,它并不总是相同的(如果事情修改DOM事件被触发之后,但在你的处理器接到电话).
小智 22
如果未在浏览器中实现,您可以创建自己的composPath函数:
function composedPath (el) {
var path = [];
while (el) {
path.push(el);
if (el.tagName === 'HTML') {
path.push(document);
path.push(window);
return path;
}
el = el.parentElement;
}
}
Run Code Online (Sandbox Code Playgroud)
返回的值等同于Google Chrome的event.path.
例:
document.getElementById('target').addEventListener('click', function(event) {
var path = event.path || (event.composedPath && event.composedPath()) || composedPath(event.target);
});
Run Code Online (Sandbox Code Playgroud)
此功能用作Event.composedPath()或用于填充Event.Path
function eventPath(evt) {
var path = (evt.composedPath && evt.composedPath()) || evt.path,
target = evt.target;
if (path != null) {
// Safari doesn't include Window, but it should.
return (path.indexOf(window) < 0) ? path.concat(window) : path;
}
if (target === window) {
return [window];
}
function getParents(node, memo) {
memo = memo || [];
var parentNode = node.parentNode;
if (!parentNode) {
return memo;
}
else {
return getParents(parentNode, memo.concat(parentNode));
}
}
return [target].concat(getParents(target), window);
}
Run Code Online (Sandbox Code Playgroud)
使用 composePath() 并使用 IE 的 polyfill: https://gist.github.com/rockinghelvetica/00b9f7b5c97a16d3de75ba99192ff05c
包含上面的文件或粘贴代码:
// Event.composedPath
(function(e, d, w) {
if(!e.composedPath) {
e.composedPath = function() {
if (this.path) {
return this.path;
}
var target = this.target;
this.path = [];
while (target.parentNode !== null) {
this.path.push(target);
target = target.parentNode;
}
this.path.push(d, w);
return this.path;
}
}
})(Event.prototype, document, window);
Run Code Online (Sandbox Code Playgroud)
然后使用:
var path = event.path || (event.composedPath && event.composedPath());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20636 次 |
| 最近记录: |