jQuery ready()的四个变体-有什么区别? [英] Four variations of jQuery ready() -- what's the difference?

查看:60
本文介绍了jQuery ready()的四个变体-有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到四种不同的方法来告诉jQuery在文档准备好后执行一个函数.这些都是等价的吗?

I've seen four different ways to tell jQuery to execute a function when the document is ready. Are these all equivalent?

$(document).ready(function () {
  alert('$(document).ready()');
});  

$().ready(function () {
  alert('$().ready()');
}); 

$(function () {
  alert('$()');
});     

jQuery(function ($) {
  alert('jQuery()');
}); 

推荐答案

没有区别.

$jQuery相同.如果您查看未缩小的来源,则会看到var $ = jQuery = ...或类似的内容.

$ is the same as jQuery. If you view the unminified source, you will see var $ = jQuery = ... or something to that effect.

jQuery函数检查其参数的类型,如果是函数,则将其与$(document).ready(...)一样对待

The jQuery function checks the type of it's parameter, if it is a function, it treats it the same as $(document).ready(...)

不带参数调用jQuery默认情况下使用document.因此$()$(document)是相同的.在Firebug中尝试.

Calling jQuery without a parameter defaults to using document. So $() and $(document) are identical. Try it in Firebug.

这篇关于jQuery ready()的四个变体-有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆