Can you extend a HTMLDivElement in TypeScript?

前端 未结 4 1261
名媛妹妹
名媛妹妹 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:25

    It is not the best way but it works.

    function MyElement(){
      var element = document.createElement("div");
    
      element.viewInner = function(){
        console.log(this, this.innerHTML)
      };
    
      return element;
    }
    
    var test = new MyElement();
    test.innerHTML = "Hi";
    test.viewInner(); // 
    Hi

提交回复
热议问题