get() 方法在 CustomElementRegistry interface returns the constructor for a previously-defined custom element.

句法

constructor = customElements.get(name);
					

参数

名称

The name of the custom element whose constructor you want to return a reference to.

返回值

The constructor for the named custom element, or undefined if there is no custom element definition with that name.

范例

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');
					

规范

规范 状态 注释
HTML 实时标准
The definition of 'customElements.get()' in that specification.
实时标准 初始定义。

浏览器兼容性

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. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
get Chrome 66 注意事项
66 注意事项
Support for 'Customized built-in elements' as well.
54 注意事项
Support for 'Autonomous custom elements' only.
Edge 79 注意事项
79 注意事项
Support for 'Customized built-in elements' as well.
79 注意事项
Support for 'Autonomous custom elements' only.
Firefox 63
63
59 — 63 Disabled
Disabled From version 59 until version 63 (exclusive): this feature is behind the dom.webcomponents.customelements.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
? — 59 Disabled
Disabled Until version 59 (exclusive): this feature is behind the dom.webcomponents.enabled preference (needs to be set to true ) 和 preference (needs to be set to ). To change preferences in Firefox, visit about:config.
IE 不支持 No Opera 53 注意事项
53 注意事项
Support for 'Customized built-in elements' as well.
41 注意事项
Support for 'Autonomous custom elements' only.
Safari 10.1 注意事项
10.1 注意事项
Supports 'Autonomous custom elements' but not 'Customized built-in elements'
WebView Android 66 注意事项
66 注意事项
Support for 'Customized built-in elements' as well.
54 注意事项
Support for 'Autonomous custom elements' only.
Chrome Android 66 注意事项
66 注意事项
Support for 'Customized built-in elements' as well.
54 注意事项
Support for 'Autonomous custom elements' only.
Firefox Android 63
63
59 — 63 Disabled
Disabled From version 59 until version 63 (exclusive): this feature is behind the dom.webcomponents.customelements.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
? — 59 Disabled
Disabled Until version 59 (exclusive): this feature is behind the dom.webcomponents.enabled preference (needs to be set to true ) 和 preference (needs to be set to ). To change preferences in Firefox, visit about:config.
Opera Android 47 注意事项
47 注意事项
Support for 'Customized built-in elements' as well.
41 注意事项
Support for 'Autonomous custom elements' only.
Safari iOS 10.3 注意事项
10.3 注意事项
Supports 'Autonomous custom elements' but not 'Customized built-in elements'
Samsung Internet Android 9.0 注意事项
9.0 注意事项
Support for 'Customized built-in elements' as well.
6.0 注意事项
Support for 'Autonomous custom elements' only.

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

元数据

  • 最后修改:
  1. CustomElementRegistry
  2. 方法
    1. define()
    2. get()
    3. upgrade()
    4. whenDefined()

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1