Instantiating new HTMLElement in TypeScript

前端 未结 3 848
庸人自扰
庸人自扰 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:11

    You have mistaken typescript syntax with c# syntax.

    Just replace

    var elem = new HTMLDivElement();
    

    with one of the following

    var elem = document.createElement('div');
    

    or

    var elem = (document.createElement('div'));
    

    or

    let elem = document.createElement('div') as HTMLDivElement
    

    (if you need to use HTMLDivElement attributes)

提交回复
热议问题