Movatterモバイル変換


[0]ホーム

URL:


  1. 面向开发者的 Web 技术
  2. Web API
  3. Window
  4. Window.customElements

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in EnglishAlways switch to English

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月.

customElementsWindow 对象上的一个只读属性,接口返回一个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

浏览器兼容性

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp