我有一个定义如下的Web组件自定义元素。
<template id="dropdown-template">
<select>
<slot></slot>
</select>
</template>
<script>
class Dropdown extends HTMLElement {
constructor() {
super();
const shadowRoot = this.attachShadow({mode: 'open'});
let template = document.getElementById('dropdown-template');
shadowRoot.appendChild(template.content.cloneNode(true));
}
}
customElements.define("drop-down", Dropdown);
</script>
当我尝试使用它时,我会尝试将带有值的选项标签传递到自定义元素中。
<drop-down>
<option>one</option>
<option>two</option>
<option>three</option>
</drop-down>
这个不起作用。选择元素显示为直接子元素是 <slot>
元素,并且没有渲染选项。使用 <select>
标签无法实现吗?