Window.customElements
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since â¨2020å¹´1æâ©.
customElements æ¯ Window 对象ä¸çä¸ä¸ªåªè¯»å±æ§ï¼æ¥å£è¿åä¸ä¸ª CustomElementRegistry 对象çå¼ç¨ï¼å¯ç¨äºæ³¨åæ°ç custom elementï¼æè
è·åä¹åå®ä¹è¿çèªå®ä¹å
ç´ çä¿¡æ¯ã
ä¾å
è¿ä¸ªå±æ§æå¸¸ç¨çä¾åæ¯ç¨æ¥è·å使ç¨CustomElementRegistry.define()æ¹æ³å®ä¹å注åçèªå®ä¹å
ç´ ï¼ä¾å¦ï¼
js
let customElementRegistry = window.customElements;
customElementRegistry.define("my-custom-element", MyCustomElement);
However, it is usually shortened to something like the following:
js
customElements.define(
"element-details",
class extends HTMLElement {
constructor() {
super();
const template = document.getElementById(
"element-details-template",
).content;
const shadowRoot = this.attachShadow({ mode: "open" }).appendChild(
template.cloneNode(true),
);
}
},
);
åé æä»¬ç web-components-examples è·åæ´å¤æç¨çä¾åã
è§è
| Specification |
|---|
| HTML > # dom-window-customelements > |