::slotted()
:slotted()
CSS 伪元素用于选定那些被放在 HTML 模板中的元素(更多请查看使用模板和插槽)。
这个伪类选择器仅仅适用于影子节点树(Shadow Dom)。并且只会选择实际的元素节点,而不包括文本节点。
css
/* Selects any element placed inside a slot */
::slotted(*) {
font-weight: bold;
}
/* Selects any <span> placed inside a slot */
::slotted(span) {
font-weight: bold;
}
语法
Error: could not find syntax for this item
样例
下面的小片段是关于 插槽伪类元素 小 demo (点击查看实例).
在这个小 demo 中,我们使用一个带有 3 个插槽的 HTML 模板:
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>
的定义如下:
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));
}
},
);
为了更好地区分未被成功填充的插槽和成功填充的插槽, 我们在 CSS 中选择了所有的插槽元素 (::slotted(*)
), 并填充了不一样的颜色和字体。结果也是如此。
元素就像如下被填充了起来:
html
<person-details>
<p slot="person-name">Dr. Shazaam</p>
<span slot="person-age">Immortal</span>
<span slot="person-occupation">Superhero</span>
</person-details>
规范
Specification |
---|
CSS Scoping Module Level 1 # slotted-pseudo |
浏览器兼容性
BCD tables only load in the browser