diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/event/composedpath | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/event/composedpath')
-rw-r--r-- | files/zh-cn/web/api/event/composedpath/index.html | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/event/composedpath/index.html b/files/zh-cn/web/api/event/composedpath/index.html new file mode 100644 index 0000000000..1ab2f9abfb --- /dev/null +++ b/files/zh-cn/web/api/event/composedpath/index.html @@ -0,0 +1,92 @@ +--- +title: Event.composedPath() +slug: Web/API/Event/composedPath +tags: + - API +translation_of: Web/API/Event/composedPath +--- +<p>{{APIRef("Shadow DOM")}}</p> + +<p> <strong><code>composedPath()</code></strong> 是 {{domxref("Event")}} 接口的一个方法,当对象数组调用该侦听器时返回事件路径。 如果影子根节点被创建并且{{domxref("ShadowRoot.mode")}}是关闭的,那么该路径不包括影子树中的节点。</p> + +<h2 id="语法"><font face="x-locale-heading-primary, zillaslab, Palatino, Palatino Linotype, x-locale-heading-secondary, serif"><span style="font-size: 40px;"><strong>语法</strong></span></font></h2> + +<pre class="syntaxbox">var <em>composed</em> = <em>Event</em>.composedPath(); +</pre> + +<h3 id="参数">参数</h3> + +<p>无.</p> + +<h3 id="返回值">返回值</h3> + +<p>一个 {{domxref("EventTarget")}}对象数组,表示将在其上调用事件侦听器的对象。</p> + +<h2 id="例子"><font face="x-locale-heading-primary, zillaslab, Palatino, Palatino Linotype, x-locale-heading-secondary, serif"><span style="font-size: 40px;"><strong>例子</strong></span></font></h2> + +<p>在我们的 composed-composed-path 例子中,我们定义了两个自定义元素,<code><open-shadow></code> 和 <code><closed-shadow>,</code>两 个全都调用了它们文本属性的内容然后作为<code><p></code> 元素的文本内容将它们插入到元素的影子DOM中。两者之间唯一的区别是它们影子的根结点是在它们的模式被分别设置成<code>open</code> 和 <code>closed</code> 的情况下连接的。</p> + +<p>第一个定义就像这样, 比如:</p> + +<pre class="brush: js">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); + + } +});</pre> + +<p>然后我们在我们的页面中插入其中一个元素:</p> + +<pre class="brush: html"><open-shadow text="I have an open shadow root"></open-shadow> +<closed-shadow text="I have a closed shadow root"></closed-shadow></pre> + +<p>然后在 <code><html></code> 元素中插入一个鼠标点击事件:</p> + +<pre class="brush: js">document.querySelector('html').addEventListener('click',function(e) { + console.log(e.composed); + console.log(e.composedPath()); +});</pre> + +<p>当你先后点击 <code><open-shadow></code> 和 <code><closed-shadow></code> 这两个元素, 你将会注意到两件事情. 第一, <code>composed</code> 这个属性返回值为 <code>true</code> 因为 <code>click</code> 事件总能够在影子边界中传播。 第二,你将注意到两个元素中<code>composedPath</code> 的值的不同。 <code><open-shadow></code> 元素的组成路径是这个:</p> + +<pre class="brush: js"><span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><span class="objectBox objectBox-array"><span class="objectTitle">Array </span><span class="arrayLeftBracket">[ </span><span class="objectBox objectBox-node"><span class="tag-name">p</span></span>, <span class="objectBox objectBox-object"><span class="objectTitle">ShadowRoot</span></span>, <span class="objectBox objectBox-node"><span class="tag-name">open-shadow</span></span>, <span class="objectBox objectBox-node"><span class="tag-name">body</span></span>, <span class="objectBox objectBox-node"><span class="tag-name">html</span></span>, <span class="objectBox objectBox-document"><span class="objectTitle">HTMLDocument</span> <span class="location">https://mdn.github.io/web-components-examples/composed-composed-path/</span></span>, <span class="objectBox objectBox-Window"><span class="objectTitle">Window</span></span><span class="arrayRightBracket"> ]</span></span></span></span></span></pre> + +<p>尽管 <code><closed-shadow></code> 元素的组成路径是像下面这样:</p> + +<pre class="brush: js"><span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><span class="objectBox objectBox-array"><span class="objectTitle">Array </span><span class="arrayLeftBracket">[ </span><span class="objectBox objectBox-node"><span class="tag-name">closed-shadow</span></span>, <span class="objectBox objectBox-node"><span class="tag-name">body</span></span>, <span class="objectBox objectBox-node"><span class="tag-name">html</span></span>, <span class="objectBox objectBox-document"><span class="objectTitle">HTMLDocument</span> <span class="location">https://mdn.github.io/web-components-examples/composed-composed-path/</span></span>, <span class="objectBox objectBox-Window"><span class="objectTitle">Window</span></span><span class="arrayRightBracket"> ]</span></span></span></span></span></pre> + +<p>在第二个例子中,事件监听器仅能够传播到 <code><closed-shadow></code> 元素本身,但是不会到影子边界内的节点。</p> + +<h2 id="规范"><font face="x-locale-heading-primary, zillaslab, Palatino, Palatino Linotype, x-locale-heading-secondary, serif"><span style="font-size: 40px;"><strong>规范</strong></span></font></h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规格</th> + <th scope="col">状态</th> + <th scope="col">评语</th> + </tr> + <tr> + <td>{{SpecName('DOM WHATWG', '##dom-event-composedpath', 'composedPath()')}}</td> + <td>{{Spec2('DOM WHATWG')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容"><font face="x-locale-heading-primary, zillaslab, Palatino, Palatino Linotype, x-locale-heading-secondary, serif"><span style="font-size: 40px;"><strong>浏览器兼容</strong></span></font></h2> + + + +<div> +<div class="hidden">这个页面的兼容表是从结构化的数据中制作出来的。如果你想为数据做贡献,请点击 <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 然后发送一个 pull 请求.</div> + +<p>{{Compat("api.Event.composedPath")}}</p> +</div> |