MouseEvent.path等效于Firefox&苹果浏览器 [英] MouseEvent.path equivalent in Firefox & Safari
问题描述
我使用Polymer 1.0,当在Chrome中点击某个按钮时,会生成一个 MouseEvent
。这个 MouseEvent
对象有一个路径
属性,这是一个父元素到点击按钮的有序数组。在Firefox&然而,Safari会产生一个点击
,它没有路径
属性。是否有与点击
对象的等价属性,它给了我相同的信息?
它不可用,但是如果你真的想拥有这个属性,那么你可以像这样扩展Event对象的本地原型:
if(!(Event.prototype中的path))
Object.defineProperty(Event.prototype,path,{
get:function(){$ b $ (currentElem){
path.push(currentElem);
currentElem = currentElem.parentElement; $ b var path = [];
var currentElem = this.target;
$ b if(path.indexOf(window)=== -1&& path.indexOf(document)=== -1)
path.push(document); $ b $ (path.indexOf(window)=== -1)
path.push(window);
return path;
}
});
然而,如果我是你,我不会扩展原型 - 我会创建一个像上面提到的函数如果你只想覆盖那些类型的事件,那么我会将Event.prototype更改为MouseEvent.prototype。
I'm using Polymer 1.0 and when there is a click on a button in Chrome a MouseEvent
is generated. This MouseEvent
object has a path
property which is an ordered array of parent elements to the clicked button. In Firefox & Safari, however, a click
is generated which does not have a path
property. Is there an equivalent property of the click
object which gives me the same information?
It's not available, but if you really would like to have this property, then you could extend the native prototype of the Event object like so:
if (!("path" in Event.prototype))
Object.defineProperty(Event.prototype, "path", {
get: function() {
var path = [];
var currentElem = this.target;
while (currentElem) {
path.push(currentElem);
currentElem = currentElem.parentElement;
}
if (path.indexOf(window) === -1 && path.indexOf(document) === -1)
path.push(document);
if (path.indexOf(window) === -1)
path.push(window);
return path;
}
});
However if I were you, I wouldn't extend the prototype - I would create a function like mentioned above instead.
Also I would change Event.prototype to MouseEvent.prototype if you want to cover only those types of events.
这篇关于MouseEvent.path等效于Firefox&苹果浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!