2
我試圖定義新的自定義元素。如何爲同一類定義更多自定義元素
// the class
class MyEl extends HTMLElement {
constructor() {
// mandatory super call to upgrade the current context
super();
// any other Class/DOM related procedure
this.setAttribute('custom-attribute', 'value');
this.innerText = 'new element';
}
}
// its mandatory definition
customElements.define('my-el', MyEl);
// the JS way to create a custom element
const newMe = new MyEl();
document.body.appendChild(newMe);
定義相同類的新元素將導致錯誤:
「未捕獲拋出:DOMException:未能執行‘規定’關於‘CustomElementRegistry’:
此構造已經被使用此註冊表「
但我想爲同一類定義新的元素。 你能否建議一種方法來顯示來自同一類(MyEl)的多個項目。
預先感謝!