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/css/_doublecolon_slotted/index.html | |
| 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/css/_doublecolon_slotted/index.html')
| -rw-r--r-- | files/zh-cn/web/css/_doublecolon_slotted/index.html | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/files/zh-cn/web/css/_doublecolon_slotted/index.html b/files/zh-cn/web/css/_doublecolon_slotted/index.html new file mode 100644 index 0000000000..4380de3dea --- /dev/null +++ b/files/zh-cn/web/css/_doublecolon_slotted/index.html @@ -0,0 +1,106 @@ +--- +title: '::slotted()' +slug: 'Web/CSS/::slotted' +translation_of: 'Web/CSS/::slotted' +--- +<div>{{ CSSRef }}</div> + +<p><strong><code>:slotted()</code></strong> <a href="/zh-CN/docs/Web/CSS">CSS</a> <a href="/zh-CN/docs/Web/CSS/Pseudo-elements">伪元素</a> 用于选定那些被放在 HTML模板 中的元素 (更多请查看 <a href="/zh-CN/docs/Web/Web_Components/Using_templates_and_slots">使用模板和插槽</a>).</p> + +<p>这个伪类选择器仅仅适用于 <a href="/en-US/docs/Web/Web_Components/Using_shadow_DOM">影子节点树(Shadow Dom)</a>. 并且只会选择实际的元素节点, 而不包括文本节点.</p> + +<pre class="brush: css no-line-numbers">/* Selects any element placed inside a slot */ +::slotted(*) { + font-weight: bold; +} + +/* Selects any <span> placed inside a slot */ +::slotted(span) { + font-weight: bold; +} +</pre> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">{{csssyntax}}</pre> + +<h2 id="样例">样例</h2> + +<p>下面的小片段是关于 <a href="https://github.com/mdn/web-components-examples/tree/master/slotted-pseudo-element">插槽伪类元素</a> 小demo (<a href="https://mdn.github.io/web-components-examples/slotted-pseudo-element/">点击查看实例</a>).</p> + +<p>在这个小demo 中, 我们使用一个带有3个插槽的HTML模板:</p> + +<pre class="brush: html"><template id="person-template"> + <div> + <h2>Personal ID Card</h2> + <slot name="person-name">NAME MISSING</slot> + <ul> + <li><slot name="person-age">AGE MISSING</slot></li> + <li><slot name="person-occupation">OCCUPATION MISSING</slot></li> + </ul> + </div> +</template></pre> + +<p>自定义元素 <code><person-details></code> 的定义如下:</p> + +<pre class="brush: js">customElements.define('person-details', + class extends HTMLElement { + constructor() { + super(); + let template = document.getElementById('person-template'); + let templateContent = template.content; + + const shadowRoot = this.attachShadow({mode: 'open'}); + + let style = document.createElement('style'); + style.textContent = 'div { padding: 10px; border: 1px solid gray; width: 200px; margin: 10px; }' + + 'h2 { margin: 0 0 10px; }' + + 'ul { margin: 0; }' + + 'p { margin: 10px 0; }' + + '::slotted(*) { color: gray; font-family: sans-serif; } '; + + shadowRoot.appendChild(style); + shadowRoot.appendChild(templateContent.cloneNode(true)); + } +})</pre> + +<p>为了更好地区分<strong>未被成功填充的插槽</strong>和<strong>成功填充的插槽</strong>, 我们在CSS中选择了所有的插槽元素(<code>::slotted(*)</code>), 并填充了不一样的颜色和字体. 结果也是如此.</p> + +<p>元素就像如下被填充了起来:</p> + +<pre class="brush: html"><person-details> + <p slot="person-name">Dr. Shazaam</p> + <span slot="person-age">Immortal</span> + <span slot="person-occupation">Superhero</span> +</person-details></pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{ SpecName('CSS Scope', '#slotted-pseudo', '::slotted') }}</td> + <td>{{ Spec2('CSS Scope') }}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + + + +<p>{{Compat("css.selectors.slotted")}}</p> + +<h2 id="参考">参考</h2> + +<ul> + <li><a href="/en-US/docs/Web/Web_Components">Web components</a></li> +</ul> |
