MouseEvent.path equivalent in Firefox & Safari

前端 未结 2 762
有刺的猬
有刺的猬 2021-02-04 00:21

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 proper

相关标签:
2条回答
  • 2021-02-04 01:02

    It seems like the e.composedPath() method might be a cross-browser version of e.path. It works in Chrome and Firefox. Not sure about Safari.

    0 讨论(0)
  • 2021-02-04 01:03

    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.

    0 讨论(0)
提交回复
热议问题