我正在尝试将我的 html 输入控件转换为 typescript 中的 HTMLInputElement。尝试通过普通的速记 jQuery 方法访问输入控件,
e.g. <HTMLInputElement> $("#CompareToControl")
导致出现波浪形红线错误“JQuery 类型和 HTMLInputElement 类型均不可相互分配。
有什么方法可以让它工作,或者我们被迫使用旧的普通方式来引用控件,例如
<HTMLInputElement> document.getElementById("CompareToControl");
最佳答案
这是因为 $(expn)
不返回 DOM 元素,而是返回 JQuery 对象,并且它们不可相互分配。因此,您需要从 jquery 对象获取 HTMLInputElement
类型的 DOM 元素。即
<HTMLInputElement> $("#CompareToControl")[0]
您正在使用的 $(expn)
的类型声明如下。 ($
的类型为 JQueryStatic)
interface JQueryStatic {
/**
* Accepts a string containing a CSS selector which is then used to match a set of elements.
*
* @param selector A string containing a selector expression
* @param context A DOM Element, Document, or jQuery to use as context
*/
(selector: string, context?: JQuery): JQuery;
}
关于jquery - 在 TypeScript 中使用 jQuery $ 前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31267592/