我正在尝试使用 Vanilla 自定义元素,但似乎无法插入样式标签:
class MyElement extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: "open"});
}
connectedCallback() {
this.shadowRoot.append(document.getElementById('my-element-template').content.cloneNode(true));
}
}
window.customElements.define("my-element", MyElement);
<template id="my-element-template">
<slot name="my-style">
<style>
p { color: red; }
</style>
</slot>
<p>Why am I still red instead of green?</p>
</template>
<my-element>
<style slot="my-style">
p { color: green; }
</style>
</my-element>