--- title: '::slotted()' slug: 'Web/CSS/::slotted' translation_of: 'Web/CSS/::slotted' ---
:slotted() CSS 伪元素 用于选定那些被放在 HTML模板 中的元素 (更多请查看 使用模板和插槽).
这个伪类选择器仅仅适用于 影子节点树(Shadow Dom). 并且只会选择实际的元素节点, 而不包括文本节点.
/* Selects any element placed inside a slot */
::slotted(*) {
font-weight: bold;
}
/* Selects any <span> placed inside a slot */
::slotted(span) {
font-weight: bold;
}
下面的小片段是关于 插槽伪类元素 小demo (点击查看实例).
在这个小demo 中, 我们使用一个带有3个插槽的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>
自定义元素 <person-details> 的定义如下:
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));
}
})
为了更好地区分未被成功填充的插槽和成功填充的插槽, 我们在CSS中选择了所有的插槽元素(::slotted(*)), 并填充了不一样的颜色和字体. 结果也是如此.
元素就像如下被填充了起来:
<person-details> <p slot="person-name">Dr. Shazaam</p> <span slot="person-age">Immortal</span> <span slot="person-occupation">Superhero</span> </person-details>
| Specification | Status | Comment |
|---|---|---|
| {{ SpecName('CSS Scope', '#slotted-pseudo', '::slotted') }} | {{ Spec2('CSS Scope') }} | Initial definition. |
{{Compat("css.selectors.slotted")}}