在 TypeScript 中获取“HTMLInputElement”类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33460430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:48:38  来源:igfitidea点击:

Get a "HTMLInputElement" type in TypeScript

typescripttypeerror

提问by codelegant

There is a <input type="checkbox" id="mainCheckbox" />,I want to use the property checkedof it.And the vscode waring Property 'checked' does not exist on type 'HTMLElement'.I know that must be type HTMLInputElement,But I can't change it,the method getElementById()is return the type HTMLElement;

有一个<input type="checkbox" id="mainCheckbox" />,我想使用它的属性checked。还有vscode警告Property 'checked' does not exist on type 'HTMLElement'。我知道必须是类型HTMLInputElement,但我不能改变它,方法getElementById()是返回类型HTMLElement

var controlCheckbox= document.getElementById("mainCheckbox"),
    addBtn = document.getElementById("btn_add"),
    container = document.getElementById("observers");
ObserverSubject.extend(new ObserverSubject.Subject(), controlCheckbox);
controlCheckbox.onclick=()=>{
    this.Notify(controlCheckbox.checked);
}

enter image description here

在此处输入图片说明

回答by Shikloshi

Try this simple cast:

试试这个简单的演员表:

var controlCheckbox = <HTMLInputElement>document.getElementById("mainCheckbox")