简体   繁体   English

在TypeScript中分配DOM对象

[英]Assigning a DOM object in TypeScript

I'm trying to assign the DOM object of a webpage to a TypeScript variable. 我正在尝试将网页的DOM对象分配给TypeScript变量。

protected DOM_obj : HTMLElement;
DOM_obj = document.defaultView;

I however seem to get the following error: 但是,我似乎得到以下错误:

Type 'Window' is not assignable to type 'HTMLElement'. “窗口”类型不能分配给“HTMLElement”类型。 Property 'accessKey' is missing in type 'Window'. “Window”类型中缺少属性“accessKey”。

How would I go about assigning the DOM object of the current window to a TypeScript variable? 我如何将当前窗口的DOM对象分配给TypeScript变量?

As you can see in the TypeScript DOM API here 正如您在此处可以在TypeScript DOM API中看到的那样

document.defaultView is of type Window . document.defaultView的类型为Window

So your DOM_obj needs to be of type Window as well: 所以你的DOM_obj也必须是Window类型:

protected DOM_obj : Window;
DOM_obj = document.defaultView;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM