当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


HTML DOM customElements upgrade()用法及代码示例


customElementsupgrade()方法升级所有的shadow-containing个自定义元素节点子树中的文档,甚至在它们连接到主文档之前。

用法:

customElements.upgrade(root);

Parameters: 

  • root:具有要升级的shadow-containing后代元素的节点实例。

返回值:此方法返回void。

例:



HTML

<!DOCTYPE HTML> 
<html> 
  
<body style="text-align:center;"> 
    <h1 style="color:green;"> 
        GeeksforGeeks 
    </h1> 
  
    <p> 
        HTML | customElements upgrade() method 
    </p> 
  
    <button onclick="Geeks()"> 
        Check 
    </button> 
  
    <button onclick="upg()">Upgrade</button> 
      
    <p id="a"></p> 
  
    <script> 
        var a = document.getElementById("a"); 
  
        const el = document 
            .createElement("gfg-custom-element"); 
  
        class CustomEl extends HTMLElement { 
            constructor() { 
                super() 
                this.attachShadow({ mode:'open' }) 
                this.shadowRoot.innerHTML = ` 
                      <h1 style="color:green;">   
                        GeeksforGeeks Custom Element Data 
                    </h1> 
                    ` 
            } 
        } 
        window.customElements.define( 
                'gfg-custom-element', CustomEl); 
                  
        function Geeks() { 
            a.innerHTML = "Upgraded:"  
                + (el instanceof CustomEl); 
        } 
        function upg() { 
            customElements.upgrade(el); 
        } 
    </script> 
</body> 
  
</html>

输出:

支持的浏览器:

  • 谷歌浏览器
  • Edge
  • Firefox
  • Safari
  • Opera




相关用法


注:本文由纯净天空筛选整理自taran910大神的英文原创作品 HTML DOM customElements upgrade() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。