Can you extend a HTMLDivElement in TypeScript?

前端 未结 4 1286
名媛妹妹
名媛妹妹 2021-02-20 12:32

I am making a DOM based game for the first time. I would like to extend HTMLDivElement, however in TypeScript, HTMLDivElement is an Interface.

I would like to do this p

4条回答
  •  北荒
    北荒 (楼主)
    2021-02-20 13:15

    A bit late, but apparently it's still not possible to extend HTMLDivElement. A simple way to solve extending a DIV: just use CSS to make a generic HTMLElement behave like a div.

    CSS

     my-element {
        display:block;
     } 
    

    Javascript

    class MyElement extends HTMLElement{
      constructor(){
          super()
          this.innerHTML = "I behave exactly like a div"
      }
    }
    
    window.customElements.define('my-element', MyElement);
    

提交回复
热议问题