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/element/slot | |
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/element/slot')
-rw-r--r-- | files/zh-cn/web/api/element/slot/index.html | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/element/slot/index.html b/files/zh-cn/web/api/element/slot/index.html new file mode 100644 index 0000000000..74189f5ab6 --- /dev/null +++ b/files/zh-cn/web/api/element/slot/index.html @@ -0,0 +1,65 @@ +--- +title: Element.slot +slug: Web/API/Element/slot +tags: + - API + - Element + - Experimental + - shadow dom + - slot +translation_of: Web/API/Element/slot +--- +<p>{{APIRef("Shadow DOM")}}</p> + +<p>{{domxref("Element")}}接口的<strong><code>slot</code></strong>属性会返回已插入元素所在的Shadow DOM slot的名称。</p> + +<p>Slot是存在于<a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components">web component</a>内部的占位符,用户可以通过slot属性在web component的内部插入自定义的标记文本。(详见<a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_templates_and_slots">Using templates and slots</a>)</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">var <em>aString</em> = <em>element</em>.slot +<em>element</em>.slot = <em>aString</em> +</pre> + +<h3 id="值">值</h3> + +<p>{{domxref("DOMString")}}.</p> + +<h2 id="示例">示例</h2> + +<p>在示例 <a href="https://github.com/mdn/web-components-examples/tree/master/simple-template">simple-template example</a> (<a href="https://mdn.github.io/web-components-examples/simple-template/">在线查看</a>)中,我们创建了一个简单的自定义元素叫做 <code><my-paragraph></code> ,并为它添加了shadow root,然后使用一个包含以 <code>my-text</code>为名称的slot的template来填充它。</p> + +<p>当 <code><my-paragraph></code> 在文档中被使用时,slot标签中的内容会被填充到拥有<code>slot="my-text"</code>属性的元素之中,我们称这种元素为slotable element。(事实上可以看作是拥有slot属性的元素被填充到了template中有<code><slot></code>标签存在的地方)请看下面的示例:</p> + +<pre class="brush: js"><my-paragraph> + <span slot="my-text">Let's have some different text!</span> +</my-paragraph></pre> + +<p>在Javascript代码中我们获取到上面代码中的span的引用,然后将对应的 <code><slot></code> 元素的引用的名称打印在控制台中。</p> + +<pre class="brush: js">let slottedSpan = document.querySelector('my-paragraph span') +console.log(slottedSpan.slot); // logs 'my-text' +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Shadow DOM','#widl-Element-slot','slot')}}</td> + <td>{{Spec2('Shadow DOM')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{Compat("api.Element.slot")}}</div> + +<div id="compat-mobile"> </div> |