jQuery 就绪函数别名

发布于 2024-10-01 15:05:14 字数 480 浏览 0 评论 0原文

我对创建新 jQuery 对象的所有不同方法有点困惑。

相关文档似乎是: http://api.jquery.com/ready/ http://api.jquery.com/jQuery/

从这两个文档中,以下内容都是等效的, (除了别名或不别名“$”之外):

  • $(document).ready(handler)
  • $().ready(handler)
  • $(handler)
  • jQuery(function($) {});
  • jQuery(文档).ready(function($) {});

这是正确的吗?我错过了什么吗?

I'm a little confused about all the different ways to create a new jQuery object.

the relevent docs seem to be:
http://api.jquery.com/ready/
http://api.jquery.com/jQuery/

From those two docs the following are all equivalent, (with the exception of aliasing or not aliasing '$'):

  • $(document).ready(handler)
  • $().ready(handler)
  • $(handler)
  • jQuery(function($) {});
  • jQuery(document).ready(function($) {});

Is that correct? Did I miss any?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苄①跕圉湢 2024-10-08 15:05:14

这些在某种程度上是等效的:

  • $(document).ready(handler) - 加载 DOM 时调整 handler
  • $().ready(handler) code> - 加载 DOM 时运行 handler已弃用,不要使用)
  • $(handler) - 运行 handler 然后加载 DOM - $(document).ready(handler) 的快捷方式
  • jQuery(function($) {}) 与上面的 #3 相同,只需使用 jQuery 而不是 $ 别名
  • jQuery(document).ready(function($) {}) - 与第一个相同,再次使用 jQuery 而不是 $ 别名

如果 $ 被定义为其他内容,例如 Prototype,那么前 3 个将不起作用。最后两个是类似的,它们只是接受传入的第一个参数(jQuery 对象)并将其设置为 $ ,即使在 >$ 是别的东西:

jQuery(function($) {
  $("input").val("something");
});

These are somewhat equivalent:

  • $(document).ready(handler) - tuns the handler when the DOM is loaded
  • $().ready(handler) - runs the handler when the DOM is loaded (deprecated, don't use)
  • $(handler) - runs the handler then the DOM is loaded - shortcut to $(document).ready(handler)
  • jQuery(function($) {}) same as #3 above, just using jQuery instead of the $ alias
  • jQuery(document).ready(function($) {}) - same as the first, again using jQuery instead of the $ alias

If $ is defined as something else, e.g. Prototype, then the first 3 won't work. The last 2 are similiar they're just accepting the first argument passed in (the jQuery object) and making it $ inside, making it possible to do this even when $ is something else:

jQuery(function($) {
  $("input").val("something");
});
只是在用心讲痛 2024-10-08 15:05:14

嗯,还有另一个。来自文档:

还有
$(document).bind("ready", handler)
这与就绪的行为类似
方法,但有一个例外:如果
Ready 事件已经触发,您
尝试 .bind("ready") 绑定
处理程序将不会被执行。

其他初始化方法将始终运行...因此您可能会发现自己在许多文件中声明 $(document).ready(function() { //stuff } ,例如,处理程序是 我总是

选择 jQuery(document).ready(function($) {})$(document).ready(function() {})通常...我发现它们更具可读性。

另一种方法是在结束正文标记之前调用脚本,并在其中执行类似的操作,

(function($) {
    //stuff
})(jQuery);

如果您需要避免与其他库发生冲突,则使用 $.是一个自动执行的匿名函数,它允许您在其范围内使用别名,而不必担心与其他库发生冲突。

Well, there is another. From the docs:

There is also
$(document).bind("ready", handler).
This behaves similarly to the ready
method but with one exception: If the
ready event has already fired and you
try to .bind("ready") the bound
handler will not be executed.

The other initialiser methods will always run... so you might find yourself declaring $(document).ready(function() { //stuff } in a number of files for example, and the handler is always run.

I'd go with jQuery(document).ready(function($) {}) or $(document).ready(function() {}) more often than not... I find that they're more readable.

Another approach would be to call a script just before the closing body tag and in it do something like,

(function($) {
    //stuff
})(jQuery);

if you need to avoid conflicts with other libraries using $. This is a self-executing anonymous function and it lets you use the alias in its scope without fear of conflicts from other libraries.

巾帼英雄 2024-10-08 15:05:14

如果您只使用 jQuery,那么 $() 相当于 jQuery()。所以这涵盖了其中的一半。

然后,如果您使用 $() 而不是 $(document).ready,则它们是相同的。在这种情况下,它只是一个辅助函数。例如,您可能想在其他东西上添加准备,在这种情况下,您会这样做: $(foo).load({})

最后,我不知道 $().ready 是什么意思,因为您必须传递一个参数。

Well if you are only using jQuery then $() is equivalent to jQuery(). So that covers half of them.

Then, if you use $() instead of $(document).ready, those are the same. In this case, it is just a helper function. You may want to add ready on something else for example, in which case, you would do: $(foo).load({})

Last, I don't know what you mean with $().ready because you have to pass a parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文