Javascript TypeScript抱怨HtmleElement没有value属性
TypeScript抱怨HtmleElement没有value属性,但当我在JavaScript中使用它时,它工作得很好Javascript TypeScript抱怨HtmleElement没有value属性,javascript,typescript,Javascript,Typescript,TypeScript抱怨HtmleElement没有value属性,但当我在JavaScript中使用它时,它工作得很好 var inputValue: HTMLElement = document.getElementById('input1'); console.log(inputValue.value); // show error message 在JavaScript中, var inputValue=document.getElementById('input1'); cons
var inputValue: HTMLElement = document.getElementById('input1');
console.log(inputValue.value); // show error message
在JavaScript中,
var inputValue=document.getElementById('input1');
console.log(inputValue.value);//它给我输入元素的值
我能知道为什么TypeScript会抱怨吗?没有值,成员有。
您需要键入断言:
var inputValue = document.getElementById('input1') as HTMLInputElement;
console.log(inputValue.value); // should be ok
编辑
typescript定义表示实际的javascript dom元素,在本例中为和。没有值,而成员有。
您需要键入断言:
var inputValue = document.getElementById('input1') as HTMLInputElement;
console.log(inputValue.value); // should be ok
编辑
typescript定义表示实际的javascript dom元素,在本例中为和。感谢您的回答。为什么是HTMLInputElement?as来自哪里?JavaScript还是TypeScript?检查修改后的答案谢谢你的回答。为什么是HTMLInputElement?as来自哪里?JavaScript还是TypeScript?检查修改后的答案