From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../zh-cn/web/api/window/customelements/index.html | 61 ++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 files/zh-cn/web/api/window/customelements/index.html (limited to 'files/zh-cn/web/api/window/customelements') diff --git a/files/zh-cn/web/api/window/customelements/index.html b/files/zh-cn/web/api/window/customelements/index.html new file mode 100644 index 0000000000..c53e5f8b48 --- /dev/null +++ b/files/zh-cn/web/api/window/customelements/index.html @@ -0,0 +1,61 @@ +--- +title: Window.customElements +slug: Web/API/Window/customElements +tags: + - API + - CustomElementRegistry + - Property + - Reference + - Web Components + - Window + - custom elements + - customElements +translation_of: Web/API/Window/customElements +--- +
{{APIRef}}
+ +

customElements 是{{domxref("Window")}}对象上的一个只读属性,接口返回一个{{domxref("CustomElementRegistry")}} 对象的引用,可用于注册新的 custom elements,或者获取之前定义过的自定义元素的信息。

+ +

例子

+ +

这个属性最常用的例子是用来获取使用{{domxref("CustomElementRegistry.define()")}}方法定义和注册的自定义元素,例如:

+ +
let customElementRegistry = window.customElements;
+customElementRegistry.define('my-custom-element', MyCustomElement);
+ +

However, it is usually shortened to something like the following:

+ +
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 获取更多有用的例子。

+ +

规范

+ + + + + + + + + + + + + + +
规范状态备注
{{SpecName("HTML WHATWG", "custom-elements.html#dom-window-customelements", "window.customElements")}}{{Spec2("HTML WHATWG")}}Initial definition.
+ +

浏览器兼容性

+ +

{{Compat("api.Window.customElements")}}

-- cgit v1.2.3-54-g00ecf