在这个例子中它们都是绿色的,我想让它们都变成红色,除了最后一个。
class MyCustomElement extends HTMLElement {
constructor(){
super();
this.attachShadow({mode: 'open'});
this.shadowRoot.innerHTML = `
<h5>Element:</h5>
<slot></slot>
<style>
:host {
color: red;
}
:host-context(:last-child) {
color: green;
}
</style>
`;
}
}
window.customElements.define('my-custom-element', MyCustomElement);
<div>
<my-custom-element>first</my-cutom-element>
<my-custom-element>... more elements</my-cutom-element>
<my-custom-element>last</my-cutom-element>
</div>
这些都是绿色的...我只希望最后一个是绿色的。
我也尝试过 :host:last-child
,但没有效果,:host-context(my-custom-element:last-child)
也会让它们全部变成绿色。