使用Firefox和Vue.js未定义event.path

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都不支持pathcomposedPath.Firefox支持composedPath.Chrome支持这两者path(这是谷歌最初的想法)和composedPath.

所以我认为你不能直接在IE11或Edge上获取路径信息.你可以很明显,获得通过的路径e.target.parentNode和每个随后parentNode,这通常是相同的,但当然点path/ composedPath是,它并不总是相同的(如果事情修改DOM事件被触发之后,但在你的处理器接到电话).

  • @Esger - 不,`composition`是[完全不同的东西](https://developer.mozilla.org/en-US/docs/Web/API/Event/composed),与`path`或`composedPath()无关`. (2认同)

小智 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)

  • 那实际上并不相同,有时parentElement在某些情况下返回null,而event.path/composePath()会给你完整的路径.. (4认同)
  • 这不是`path` /`composedPath`的实现。它错过了它们的关键方面(您不能进行polyfill)。 (2认同)

Mr.*_*r.7 7

此功能用作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)


Kom*_*mal 5

使用 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)