CustomElementRegistry: get() 方法

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

CustomElementRegistry 接口的 get() 方法返回先前定义的自定义元素的构造函数。

语法

js
get(name)

参数

name

自定义元素的名称。

返回值

给定名称的自定义元素的构造函数,如果没有定义具有该名称的自定义元素,则返回 undefined

示例

js
customElements.define(
  "my-paragraph",
  class extends HTMLElement {
    constructor() {
      let templateContent = document.getElementById("custom-paragraph").content;
      super() // returns element this scope
        .attachShadow({ mode: "open" }) // sets AND returns this.shadowRoot
        .append(templateContent.cloneNode(true));
    }
  },
);

// Return a reference to the my-paragraph constructor
let ctor = customElements.get("my-paragraph");

规范

规范
HTML
# dom-customelementregistry-get-dev

浏览器兼容性