blob: cae91370dd39cea9209e1c91add5a6cbc292fab4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
---
title: Document.registerElement()
slug: Web/API/Document/registerElement
tags:
- API
- DOM
- JavaScript
- 自訂標籤
translation_of: Web/API/Document/registerElement
---
<p>{{APIRef("DOM")}}{{Deprecated_header}}</p>
<div class="warning">
<p><strong>Warning: </strong>document.registerElement() 已經被棄用,建議使用 <a href="/en-US/docs/Web/API/CustomElementRegistry/define">customElements.define()</a>.</p>
</div>
<p>{{draft}}</p>
<p> <code><strong>document.registerElement()</strong></code> 可以在瀏覽器中註冊一個新的自訂標籤(元素)and returns a constructor for the new element.</p>
<div class="note">
<p><strong>Note:</strong> This is an experimental technology. The browser you use it in must support Web Components. See <a href="/en-US/docs/Web/Web_Components#Enabling_Web_Components_in_Firefox">Enabling Web Components in Firefox</a>.</p>
</div>
<h2 id="語法">語法</h2>
<pre class="syntaxbox">var <em>constructor</em> = document.registerElement(<em>tag-name</em>, <em>options</em>);</pre>
<h3 id="參數">參數</h3>
<dl>
<dt><em>標籤名稱</em></dt>
<dd>自訂的標籤名稱需有一個 橫線 ( - ), 例如<code>my-tag</code>.</dd>
<dt><em>options {{optional_inline}}</em></dt>
<dd>
<p>An object with properties <strong>prototype</strong> to base the custom element on, and <strong>extends</strong>, an existing tag to extend. Both of these are optional.</p>
</dd>
</dl>
<h2 id="例子">例子</h2>
<p>這是一個非常簡單的例子:</p>
<pre class="brush: js">var Mytag = document.registerElement('my-tag');
</pre>
<p>現在新的標籤已經在瀏覽器中註冊了. The <code>Mytag</code> variable holds a constructor that you can use to create a <code>my-tag</code> element in the document as follows:</p>
<pre class="brush: js">document.body.appendChild(new Mytag());</pre>
<p>This inserts an empty <code>my-tag</code> element that will be visible if you use the browser's developer tools. It will not be visible if you use the browser's view source capability. And it won't be visible in the browser unless you add some content to the tag. Here is one way to add content to the new tag:</p>
<pre class="brush: js">var mytag = document.getElementsByTagName("my-tag")[0];
mytag.textContent = "I am a my-tag element.";
</pre>
<h2 id="瀏覽器支援性">瀏覽器支援性</h2>
{{Compat("api.Document.registerElement")}}
<h2 id="也看一下">也看一下</h2>
<ul>
<li><a dir="ltr" href="/en-US/docs/Web/Web_Components/Custom_Elements">Custom Elements</a></li>
</ul>
|