问题描述
可能的重复:
$(function(){ }) 的区别是什么;和 $(document).ready(function() { }) ;?
$(document).ready(function(){})
和 $(function(){})
有什么区别我应该用 $
形式还是新的 jQuery(document).ready(function(){ })
方式编写它?
What are the differences between $(document).ready(function(){})
vs $(function(){})
and should I write it in the $
form or the new jQuery(document).ready(function(){ })
way?
如果我加载了 google api 是 google.setOnLoadCallback(function() {
更好的方法吗?还是同样的事情?
If I have google api loaded is google.setOnLoadCallback(function() {
a better way? or is it the same thing?
我也看到人们使用 $(function($){})
谁能帮帮我,我迷路了.当我不知道我写的代码时,我会打扰我.我想我应该通读图书馆.是否所有都被定义为同一件事?
Can somebody help me, I'm lost. I bugs me when I don't know the code I write. I guess I should read through the library. Are the all being defined as the same thing?
推荐答案
两种方式是等价的,我个人更喜欢第二种,$(function() {});
只是一个快捷方式准备好文档.
The two ways are equivalent, I personally prefer the second, $(function() {});
it's just a shortcut for document ready.
关于 new jQuery(document)...
构造,您实际上并不需要使用 new
运算符,如果您不这样做,jQuery 将在内部使用它'
About the new jQuery(document)...
construct, you don't really need to use the new
operator, jQuery will use it internally if you don't.
ready
处理函数接收的参数是 jQuery 对象本身.
The argument that the ready
handler function receives, is the jQuery object itself.
当您必须以与其他库兼容的模式运行 jQuery 时,这非常有用,例如:
That's quite useful where you have to run jQuery in compatibility mode with other libraries, for example:
jQuery(function ($) {
// use $ here
});
回调中的 $
参数将引用 jQuery 对象,在该函数之外它可能引用另一个库,如 PrototypeJS.
The $
argument inside the callback will refer to the jQuery object, outside that function it might refer to another library like PrototypeJS.
这篇关于$(document).ready(function() VS $(function(){的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2