Document.registerElement()

已棄用: 不推薦使用此功能。雖可能有一些瀏覽器仍然支援它,但也許已自相關的網頁標準中移除、正準備移除、或僅為了維持相容性而保留。避免使用此功能,盡可能更新現有程式;請參考頁面底部的相容性表格來下決定。請注意:本功能可能隨時停止運作。

警告: document.registerElement() 已經被棄用,建議使用 customElements.define().

document.registerElement() 可以在瀏覽器中註冊一個新的自訂標籤(元素)and returns a constructor for the new element.

備註: This is an experimental technology. The browser you use it in must support Web Components. See Enabling Web Components in Firefox (en-US).

語法

var constructor = document.registerElement(tag-name, options);

參數

標籤名稱

自訂的標籤名稱需有一個 橫線 ( - ), 例如my-tag.

options 選擇性

An object with properties prototype to base the custom element on, and extends, an existing tag to extend. Both of these are optional.

例子

這是一個非常簡單的例子:

js
var Mytag = document.registerElement("my-tag");

現在新的標籤已經在瀏覽器中註冊了. The Mytag variable holds a constructor that you can use to create a my-tag element in the document as follows:

js
document.body.appendChild(new Mytag());

This inserts an empty my-tag 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:

js
var mytag = document.getElementsByTagName("my-tag")[0];
mytag.textContent = "I am a my-tag element.";

瀏覽器支援性

BCD tables only load in the browser

也看一下