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/customelementregistry/index.html | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 files/zh-cn/web/api/customelementregistry/index.html (limited to 'files/zh-cn/web/api/customelementregistry/index.html') diff --git a/files/zh-cn/web/api/customelementregistry/index.html b/files/zh-cn/web/api/customelementregistry/index.html new file mode 100644 index 0000000000..4a2279fbdf --- /dev/null +++ b/files/zh-cn/web/api/customelementregistry/index.html @@ -0,0 +1,92 @@ +--- +title: CustomElementRegistry +slug: Web/API/CustomElementRegistry +tags: + - API + - CustomElementRegistry + - Interface + - Web Components +translation_of: Web/API/CustomElementRegistry +--- +
{{DefaultAPISidebar("Web Components")}}
+ +

CustomElementRegistry接口提供注册自定义元素和查询已注册元素的方法。要获取它的实例,请使用 {{domxref("window.customElements")}}属性。

+ +

方法

+ +
+
{{domxref("CustomElementRegistry.define()")}}
+
定义一个新的自定义元素
+
{{domxref("CustomElementRegistry.get()")}}
+
返回指定自定义元素的构造函数,如果未定义自定义元素,则返回undefined
+
{{domxref("CustomElementRegistry.upgrade()")}}
+
Upgrades a custom element directly, even before it is connected to its shadow root.
+
{{domxref("CustomElementRegistry.whenDefined()")}}
+
返回当使用给定名称定义自定义元素时将会执行的 {{jsxref("Promise", "promise")}}。(如果已经定义了这样一个自定义元素,那么立即执行返回的 promise。)
+
+ +

示例

+ +

以下代码来自我们的 word-count-web-component 示例(see it live also)。 请注意我们如何使用 {{domxref("CustomElementRegistry.define()")}} 方法在创建其类后定义自定义元素。

+ +
// Create a class for the element
+class WordCount extends HTMLParagraphElement {
+  constructor() {
+    // Always call super first in constructor
+    super();
+
+    // count words in element's parent element
+    var wcParent = this.parentNode;
+
+    function countWords(node){
+      var text = node.innerText || node.textContent
+      return text.split(/\s+/g).length;
+    }
+
+    var count = 'Words: ' + countWords(wcParent);
+
+    // Create a shadow root
+    var shadow = this.attachShadow({mode: 'open'});
+
+    // Create text node and add word count to it
+    var text = document.createElement('span');
+    text.textContent = count;
+
+    // Append it to the shadow root
+    shadow.appendChild(text);
+
+
+    // Update count when element content changes
+    setInterval(function() {
+      var count = 'Words: ' + countWords(wcParent);
+      text.textContent = count;
+    }, 200)
+
+  }
+}
+
+// Define the new element
+customElements.define('word-count', WordCount, { extends: 'p' });
+ +

规范

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

浏览器兼容性

+ + + +

{{Compat("api.CustomElementRegistry")}}

-- cgit v1.2.3-54-g00ecf