例如,我希望能够用 HTML 编写这个:
<my-container>
Some text.
</my-container>
JS:
class MyContainer extends HTMLElement {
constructor() {
super()
const shadow = this.attachShadow({mode: 'open'})
const p = document.createElement('p')
shadow.appendChild(p)
}
}
customElements.define('my-container', MyContainer)
我最终得到的(虽然并不意外):
<my-container>
<p></p>
Some text.
</my-container>
我想要的是:
<my-container>
<p>Some text.</p>
</my-container>