composedPath() 方法在 事件 interface returns the event’s path which is an array of the objects on which listeners will be invoked. This does not include nodes in shadow trees if the shadow root was created with its ShadowRoot.mode closed.

句法

var composed = Event.composedPath();
					

参数

None.

返回值

An array of EventTarget objects representing the objects on which an event listener will be invoked.

范例

In our composed-composed-path example (see it live), we define two trivial custom elements, <open-shadow> and <closed-shadow> , both of which take the contents of their text attribute and insert them into the element's shadow DOM as the text content of a <p> element. The only difference between the two is that their shadow roots are attached with their modes set to open and closed 分别。

The first definition looks like this, for example:

customElements.define('open-shadow',
  class extends HTMLElement {
    constructor() {
      super();
      let pElem = document.createElement('p');
      pElem.textContent = this.getAttribute('text');
      let shadowRoot = this.attachShadow({mode: 'open'})
        .appendChild(pElem);
  }
});
					

We then insert one of each element into our page:

<open-shadow text="I have an open shadow root"></open-shadow>
<closed-shadow text="I have a closed shadow root"></closed-shadow>
					

Then include a click event listener on the <html> 元素:

document.querySelector('html').addEventListener('click',function(e) {
  console.log(e.composed);
  console.log(e.composedPath());
});
					

When you click on the <open-shadow> element and then the <closed-shadow> element, you'll notice two things. First, the composed property returns true beause the click event is always able to propagate across shadow boundaries. Second, you'll notice a difference in the value of composedPath for the two elements. The <open-shadow> element's composed path is this:

Array [ p, ShadowRoot, open-shadow, body, html, HTMLDocument https://mdn.github.io/web-components-examples/composed-composed-path/, Window ]
					

Whereas the <closed-shadow> element's composed path is a follows:

Array [ closed-shadow, body, html, HTMLDocument https://mdn.github.io/web-components-examples/composed-composed-path/, Window ]
					

In the second case, the event listeners only propagate as far as the <closed-shadow> element itself, but not to the nodes inside the shadow boundary.

规范

规范 状态 注释
DOM
The definition of 'composedPath()' in that specification.
实时标准

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
composedPath Chrome 53
53
50 — 53 Alternate Name
Alternate Name Uses the non-standard name: deepPath
Edge 79 Firefox 52 IE 不支持 No Opera 40
40
37 — 40 Alternate Name
Alternate Name Uses the non-standard name: deepPath
Safari 10 WebView Android 53
53
50 — 53 Alternate Name
Alternate Name Uses the non-standard name: deepPath
Chrome Android 53
53
50 — 53 Alternate Name
Alternate Name Uses the non-standard name: deepPath
Firefox Android 52 Opera Android 41
41
37 — 41 Alternate Name
Alternate Name Uses the non-standard name: deepPath
Safari iOS 10 Samsung Internet Android 6.0
6.0
5.0 — 6.0 Alternate Name
Alternate Name Uses the non-standard name: deepPath

图例

完整支持

完整支持

不支持

不支持

使用非标名称。

使用非标名称。

元数据

  • 最后修改: