在TypeScript中不能使用`$ {document).ready` [英] Cannot use `$(document).ready` in TypeScript
问题描述
在TypeScript中使用$(document)
会给出错误Supplied parameters do not match any signature of call target.
Using $(document)
in TypeScript gives the error Supplied parameters do not match any signature of call target.
我正在使用TypeScript 3.1,jQuery 3.3.1和@ types/jQuery 3.3.29.
I'm using TypeScript 3.1, jQuery 3.3.1 and @types/jQuery 3.3.29.
$(document)
是否已被弃用,我应该使用其他方法还是类型定义文件中的错误?
Is $(document)
deprecated and I should use something else or is it an error in the type definition file?
此TypeScript文件的整个主体基本上是"Hello World!".
The entire body of this TypeScript file is basically "Hello World!".
$(document).ready(() => {
console.log("Hello World!");
});
推荐答案
$(document).ready(handler)
具有两个功能等效的变体,第一个是$().ready(handler)
,第二个是直接的$(handler)
.
$(document).ready(handler)
has two functionally equivalent variants, first is $().ready(handler)
, and second is direct $(handler)
.
在jQuery 3.0中,不建议使用前两个,仅保留$(handler)
.官方的理由是:
In jQuery 3.0 first two were deprecated, leaving only $(handler)
. The official justification is:
这是因为选择与
.ready()
方法的行为无关,效率低下并且可能导致对该方法行为的错误假设.
This is because selection has no bearing on the behavior of the
.ready()
method, which is inefficient and can lead to incorrect assumptions about the method's behavior.
TypeScript定义文件只是不包含不建议使用的语法,为了向后兼容,该语法仍然有效.您的脚本应如下所示:
TypeScript definition files just don't include deprecated syntax, which still works for the sake of backward compatibility. Your script should look like this:
$(() => {
console.log("Hello World!");
});
这篇关于在TypeScript中不能使用`$ {document).ready`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!