CustomElementRegistry.get()
get()
は CustomElementRegistry
インターフェイスのメソッドで、以前定義したカスタム要素のコンストラクターを返します。
構文
constructor = customElements.get(name);
引数
- name
-
コンストラクターの参照を返したいカスタム要素の名前です。
返値
名前付きのカスタム要素のコンストラクター、またはその名前のカスタム要素がない場合は undefined
です。
例
customElements.define('my-paragraph',
class extends HTMLElement {
constructor() {
let templateContent = document.getElementById('my-paragraph').content;
super() // このスコープの要素を返す
.attachShadow({mode: 'open'}) // this.shadowRoot を設定して返す
.append(templateContent.cloneNode(true));
}
})
// my-paragraph のコンストラクターの参照を返す
let ctor = customElements.get('my-paragraph');
仕様書
Specification |
---|
HTML Standard # dom-customelementregistry-get-dev |
ブラウザーの互換性
BCD tables only load in the browser