CustomElementRegistry: get() method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Phương thức get() của giao diện CustomElementRegistry trả về constructor cho một custom element đã được định nghĩa trước đó.

Cú pháp

js
get(name)

Tham số

name

Tên của custom element.

Giá trị trả về

Constructor cho custom element có tên đã cho, hoặc undefined nếu không có custom element nào được định nghĩa với tên đó.

Ví dụ

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

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

Thông số kỹ thuật

Specification
HTML
# dom-customelementregistry-get-dev

Khả năng tương thích trình duyệt