aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/customelementregistry/define
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/customelementregistry/define
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/customelementregistry/define')
-rw-r--r--files/zh-cn/web/api/customelementregistry/define/index.html205
1 files changed, 205 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>&lt;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."&gt;</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>&lt;p is="word-count"&gt;&lt;/p&gt;</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>