我正在检查某些元素的输入类型,例如 TS 中的复选框。现在我确定我有一个复选框元素,所以这个元素应该有属性检查。但如果我只是做
element: HTMLElement
if (element.InputType.toLowerCase() == "checkbox"){
element.checked = true;
}
它正在工作,但 element.checked 带有红色下划线。我认为我只需将 HTMLElement 重新键入为 CheckboxElement 之类的内容,但没有找到适合此转换的内容。如何摆脱这个?在 element.value 的情况下,我也遇到了这个问题
最佳答案
没有“checkbox”元素类型,因为它只是一个类型为checkbox
的“input”元素。您可以使用/断言 HTMLInputElement
类型,它是 HTMLElement
的扩展:
var element: HTMLInputElement;
//... You still need to do all the null checks as necessary
//The below check may be irrelevant depending upon what you are actually doing.
//But i am just adding here to show that you need to refer to the property "type" and
//not "InputType"
if (element.type.toLowerCase() == "checkbox") {
element.checked = true;
}
关于checkbox - typescript 如何判断该元素是复选框,所以 element.checked 不是红色下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33242324/