MouseEvent.path等效于Firefox&苹果浏览器 [英] MouseEvent.path equivalent in Firefox & Safari

查看:280
本文介绍了MouseEvent.path等效于Firefox&苹果浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆