diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/customelementregistry | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/customelementregistry')
5 files changed, 533 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/customelementregistry/define/index.html b/files/zh-cn/web/api/customelementregistry/define/index.html new file mode 100644 index 0000000000..b03cdd8e54 --- /dev/null +++ b/files/zh-cn/web/api/customelementregistry/define/index.html @@ -0,0 +1,205 @@ +--- +title: CustomElementRegistry.define() +slug: Web/API/CustomElementRegistry/define +tags: + - API + - CustomElementRegistry + - Web Components + - custom elements +translation_of: Web/API/CustomElementRegistry/define +--- +<p>{{APIRef("CustomElementRegistry")}}</p> + +<p>{{domxref("CustomElementRegistry")}}接口的<code><strong>define()</strong></code>方法定义了一个自定义元素。</p> + +<p>你可以创建两种类型的自定义元素:</p> + +<ul> + <li><strong>自主定制元素</strong>:独立元素; 它们不会从内置HTML元素继承。</li> + <li><strong>自定义内置元素</strong>:这些元素继承自 - 并扩展 - 内置HTML元素</li> +</ul> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">customElements.define(<em>name</em>, <em>constructor</em>, <em>options</em>); +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>name</dt> + <dd>自定义元素名.</dd> + <dt>constructor</dt> + <dd>自定义元素构造器.</dd> + <dt>options {{optional_inline}}</dt> + <dd>控制元素如何定义. 目前有一个选项支持: + <ul> + <li><code>extends</code>. 指定继承的已创建的元素. 被用于创建自定义元素.</li> + </ul> + </dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>空</p> + +<h2 id="示例">示例</h2> + +<h3 id="自主定制元素">自主定制元素</h3> + +<p>以下代码取自我们的 <a href="https://github.com/mdn/web-components-examples/tree/master/popup-info-box-web-component">popup-info-box-web-component</a> 示例(<a href="https://mdn.github.io/web-components-examples/popup-info-box-web-component/">see it live also</a>)。</p> + +<pre class="brush: js">// Create a class for the element +class PopUpInfo extends HTMLElement { + constructor() { + // Always call super first in constructor + super(); + + // Create a shadow root + var shadow = this.attachShadow({mode: 'open'}); + + // Create spans + var wrapper = document.createElement('span'); + wrapper.setAttribute('class','wrapper'); + var icon = document.createElement('span'); + icon.setAttribute('class','icon'); + icon.setAttribute('tabindex', 0); + var info = document.createElement('span'); + info.setAttribute('class','info'); + + // Take attribute content and put it inside the info span + var text = this.getAttribute('text'); + info.textContent = text; + + // Insert icon + var imgUrl; + if(this.hasAttribute('img')) { + imgUrl = this.getAttribute('img'); + } else { + imgUrl = 'img/default.png'; + } + var img = document.createElement('img'); + img.src = imgUrl; + icon.appendChild(img); + + // Create some CSS to apply to the shadow dom + var style = document.createElement('style'); + + style.textContent = '.wrapper {' + + 'position: relative;' + + '}' + + + '.info {' + + 'font-size: 0.8rem;' + + 'width: 200px;' + + 'display: inline-block;' + + 'border: 1px solid black;' + + 'padding: 10px;' + + 'background: white;' + + 'border-radius: 10px;' + + 'opacity: 0;' + + 'transition: 0.6s all;' + + 'position: absolute;' + + 'bottom: 20px;' + + 'left: 10px;' + + 'z-index: 3;' + + '}' + + + 'img {' + + 'width: 1.2rem' + + '}' + + + '.icon:hover + .info, .icon:focus + .info {' + + 'opacity: 1;' + + '}'; + + // attach the created elements to the shadow dom + + shadow.appendChild(style); + shadow.appendChild(wrapper); + wrapper.appendChild(icon); + wrapper.appendChild(info); + } +} + +// Define the new element +customElements.define('popup-info', PopUpInfo);</pre> + +<pre><code><popup-info img="img/alt.png" text="Your card validation code (CVC) is an extra + security feature — it is the last 3 or 4 + numbers on the back of your card."></code></pre> + +<div class="note"> +<p><strong>注意</strong>:自主自定义元素的构造函数必须扩展{{domxref("HTMLElement")}}。</p> +</div> + +<h3 id="自定义内置元素">自定义内置元素</h3> + +<p>以下代码取自我们的 <a href="https://github.com/mdn/web-components-examples/tree/master/word-count-web-component">word-count-web-component</a> 实例 (<a href="https://mdn.github.io/web-components-examples/word-count-web-component/">查看实例效果</a>).</p> + +<pre class="brush: js">// 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' });</pre> + +<pre class="brush: html"><code><p is="word-count"></p></code> +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName("HTML WHATWG", "custom-elements.html#dom-customelementregistry-define", "customElements.define()")}}</td> + <td>{{Spec2("HTML WHATWG")}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容">浏览器兼容</h2> + +<div class="hidden"> +<p>The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> +</div> + +<p>{{Compat("api.CustomElementRegistry.define")}}</p> diff --git a/files/zh-cn/web/api/customelementregistry/get/index.html b/files/zh-cn/web/api/customelementregistry/get/index.html new file mode 100644 index 0000000000..5b6cb1b564 --- /dev/null +++ b/files/zh-cn/web/api/customelementregistry/get/index.html @@ -0,0 +1,72 @@ +--- +title: CustomElementRegistry.get() +slug: Web/API/CustomElementRegistry/get +tags: + - CustomElementRegistry + - Experimental + - Web Components + - custom elements +translation_of: Web/API/CustomElementRegistry/get +--- +<p>{{APIRef("CustomElementRegistry")}}</p> + +<p><span class="seoSummary"> {{domxref("CustomElementRegistry")}} 的<code><strong>get()</strong></code>方法返回以前定义自定义元素的构造函数.</span></p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><em>constructor</em> = customElements.get(<em>name</em>); +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>name</dt> + <dd>你想要返回引用的构造函数的自定义元素的名字。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>指定名字的自定义元素的构造函数,如果没有使用该名称的自定义元素定义,则为<code>undefined</code>。</p> + +<h2 id="例子">例子</h2> + +<pre class="brush: js">customElements.define('my-paragraph', + class extends HTMLElement { + constructor() { + super(); + let template = document.getElementById('my-paragraph'); + let templateContent = template.content; + + const shadowRoot = this.attachShadow({mode: 'open'}) + .appendChild(templateContent.cloneNode(true)); + } +}) + +// Return a reference to the my-paragraph constructor +<span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body">let ctor = customElements.get('my-paragraph');</span></span></span> +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName("HTML WHATWG", "custom-elements.html#dom-customelementregistry-get", "customElements.get()")}}</td> + <td>{{Spec2("HTML WHATWG")}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("api.CustomElementRegistry.get")}}</p> +</div> 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 +--- +<div>{{DefaultAPISidebar("Web Components")}}</div> + +<p><strong><code>CustomElementRegistry</code></strong>接口提供注册自定义元素和查询已注册元素的方法。要获取它的实例,请使用 {{domxref("window.customElements")}}属性。</p> + +<h2 id="方法">方法</h2> + +<dl> + <dt>{{domxref("CustomElementRegistry.define()")}}</dt> + <dd>定义一个新的<a href="/zh-CN/docs/Web/Web_Components/Custom_Elements">自定义元素</a>。</dd> + <dt>{{domxref("CustomElementRegistry.get()")}}</dt> + <dd>返回指定自定义元素的构造函数,如果未定义自定义元素,则返回<code>undefined</code>。</dd> + <dt>{{domxref("CustomElementRegistry.upgrade()")}}</dt> + <dd>Upgrades a custom element directly, even before it is connected to its shadow root.</dd> + <dt>{{domxref("CustomElementRegistry.whenDefined()")}}</dt> + <dd>返回当使用给定名称定义自定义元素时将会执行的 {{jsxref("Promise", "promise")}}。(如果已经定义了这样一个自定义元素,那么立即执行返回的 promise。)</dd> +</dl> + +<h2 id="示例">示例</h2> + +<p>以下代码来自我们的 <a href="https://github.com/mdn/web-components-examples/tree/master/word-count-web-component">word-count-web-component</a> 示例(<a href="https://mdn.github.io/web-components-examples/word-count-web-component/">see it live also</a>)。 请注意我们如何使用 {{domxref("CustomElementRegistry.define()")}} 方法在创建其类后定义自定义元素。</p> + +<pre class="brush: js">// 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' });</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">备注</th> + </tr> + <tr> + <td>{{SpecName("HTML WHATWG", "custom-elements.html#customelementregistry", "CustomElementRegistry")}}</td> + <td>{{Spec2("HTML WHATWG")}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.CustomElementRegistry")}}</p> diff --git a/files/zh-cn/web/api/customelementregistry/upgrade/index.html b/files/zh-cn/web/api/customelementregistry/upgrade/index.html new file mode 100644 index 0000000000..412485c9d6 --- /dev/null +++ b/files/zh-cn/web/api/customelementregistry/upgrade/index.html @@ -0,0 +1,64 @@ +--- +title: CustomElementRegistry.upgrade() +slug: Web/API/CustomElementRegistry/upgrade +translation_of: Web/API/CustomElementRegistry/upgrade +--- +<p>{{APIRef("CustomElementRegistry")}}</p> + +<p>CustomElementRegistry接口的upgrade()方法将更新节点子树中所有包含阴影的自定义元素,甚至在它们连接到主文档之前也是如此。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">customElements.upgrade(<em>root</em>); +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>root</code></dt> + <dd>待升级的包含阴影的派生元素<code>节点</code> 。如果没有可升级的派生实例,则不会抛出异常。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>空.</p> + +<h2 id="示例">示例</h2> + +<p>摘至<a href="https://html.spec.whatwg.org/multipage/custom-elements.html#dom-customelementregistry-upgrade">HTML 规范</a>:</p> + +<pre class="brush: js">const el = document.createElement("spider-man"); + +class SpiderMan extends HTMLElement {} +customElements.define("spider-man", SpiderMan); + +console.assert(!(el instanceof SpiderMan)); // not yet upgraded + +customElements.upgrade(el); +console.assert(el instanceof SpiderMan); // upgraded! +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName("HTML WHATWG", "custom-elements.html#dom-customelementregistry-upgrade", "customElements.upgrade()")}}</td> + <td>{{Spec2("HTML WHATWG")}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器支持">浏览器支持</h2> + +<div> + + +<p>{{Compat("api.CustomElementRegistry.upgrade")}}</p> +</div> diff --git a/files/zh-cn/web/api/customelementregistry/whendefined/index.html b/files/zh-cn/web/api/customelementregistry/whendefined/index.html new file mode 100644 index 0000000000..8c5452386d --- /dev/null +++ b/files/zh-cn/web/api/customelementregistry/whendefined/index.html @@ -0,0 +1,100 @@ +--- +title: CustomElementRegistry.whenDefined() +slug: Web/API/CustomElementRegistry/whenDefined +translation_of: Web/API/CustomElementRegistry/whenDefined +--- +<p>{{APIRef("CustomElementRegistry")}}</p> + +<p><span class="seoSummary"> 当一个元素被定义时{{domxref("CustomElementRegistry")}} 中的方法<code><strong>whenDefined()</strong></code> 接口返回 {{jsxref("Promise")}}.</span></p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">Promise<> customElements.whenDefined(<em>name</em>);</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>name</dt> + <dd>自定义元素的名称</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>当自定义元素被定义时一个{{jsxref("Promise")}} 返回{jsxref("undefined")}}. 如果自定义元素已经被定义,则resolve立即执行。</p> + +<dl> +</dl> + +<h3 id="例外">例外</h3> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Exception</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>SyntaxError</code></td> + <td>如果提供的 <strong>name</strong> 不是一个有效的 <a href="https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name">自定义元素名字</a>, promise 的 reject 回调会接收到一个 <code>SyntaxError</code>.</td> + </tr> + </tbody> +</table> + +<h2 id="例子">例子</h2> + +<p><code><font face="Open Sans, arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">这个例子使用</span></font>whenDefined()</code> 来检测生成菜单的自定义元素何时被定义. 这个菜单显示占位符内容一直到菜单内容已经准备好显示.</p> + +<pre class="brush: html"><nav id="menu-container"> + <div class="menu-placeholder">Loading...</div> + <nav-menu> + <menu-item>Item 1</menu-item> + <menu-item>Item 2</menu-item> + ... + <menu-item>Item N</menu-item> + </nav-menu> +</nav> +</pre> + +<pre class="brush: js">const container = document.getElementById('menu-container'); +const placeholder = container.querySelector('.menu-placeholder'); +// Fetch all the children of menu that are not yet defined. +const undefinedElements = container.querySelectorAll(':not(:defined)'); + +const promises = [...undefinedElements].map( + button => customElements.whenDefined(button.localName) +); + +// Wait for all the children to be upgraded, +// then remove the placeholder. +await Promise.all(promises); +container.removeChild(placeholder); +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName("HTML WHATWG", "custom-elements.html#dom-customelementregistry-define", "customElements.whenDefined()")}}</td> + <td>{{Spec2("HTML WHATWG")}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> +<div> + + +<p>{{Compat("api.CustomElementRegistry.whenDefined")}}</p> +</div> +</div> |