CustomElementRegistry.get()
CustomElementRegistry
的 get()
方法返回以前定义自定义元素的构造函数。
语法
constructor = customElements.get(name);
参数
- name
-
你想要返回引用的构造函数的自定义元素的名字。
返回值
指定名字的自定义元素的构造函数,如果没有使用该名称的自定义元素定义,则为undefined
。
例子
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
let ctor = customElements.get("my-paragraph");
规范
Specification |
---|
HTML # dom-customelementregistry-get-dev |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
get |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- Partial support
- Partial support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.