Instantiating new HTMLElement in TypeScript

前端 未结 3 844
庸人自扰
庸人自扰 2021-02-04 23:44

I am trying to instantiate new HTMLDivElement in TypeScript

var elem = new HTMLDivElement();

but the browser throws

Uncaught T         


        
3条回答
  •  醉梦人生
    2021-02-05 00:03

    If you extend HTMLElement or any subclass like HTMLDivElement

    You also need to register the custom element

    
    class SampleDiv extends HTMLDivElement{
        constructor(){
           super();
        }
    }
    customElements.define('sample-div', SampleDiv, { extends: 'div' });
    

    See more here: https://javascript.info/custom-elements

提交回复
热议问题