Javascript $(document).ready(function() VS $(function(){
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3528509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$(document).ready(function() VS $(function(){
提问by CMS
Possible Duplicate:
what is difference of $(function(){ }); and $(document).ready(function() { }) ;?
可能的重复:
$(function(){ }) 的区别是什么;和 $(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?
$(document).ready(function(){})vs之间有什么区别,$(function(){})我应该以$形式还是以新jQuery(document).ready(function(){ })方式编写?
If I have google api loaded is google.setOnLoadCallback(function() {a better way? or is it the same thing?
如果我加载google.setOnLoadCallback(function() {了google api 是更好的方法吗?还是同样的事情?
I've also seen people use $(function($){})
我也看到有人用 $(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?
有人可以帮助我吗,我迷路了。当我不知道我写的代码时,我会打扰我。我想我应该通读图书馆。所有都被定义为同一件事吗?
回答by CMS
The two ways are equivalent, I personally prefer the second, $(function() {});it's just a shortcutfor document ready.
这两种方式是等价的,我个人更喜欢第二种,$(function() {});它只是准备文档的快捷方式。
About the new jQuery(document)...construct, you don't really need to use the newoperator, jQuery will use it internally if you don't.
关于new jQuery(document)...构造,您实际上并不需要使用new运算符,如果您不使用,jQuery 将在内部使用它。
The argument that the readyhandler function receives, is the jQuery object itself.
ready处理函数接收的参数是 jQuery 对象本身。
That's quite useful where you have to run jQuery in compatibility mode with other libraries, for example:
当您必须以与其他库兼容的模式运行 jQuery 时,这非常有用,例如:
jQuery(function ($) {
// use $ here
});
The $argument inside the callback will refer to the jQuery object, outside that function it might refer to another library like PrototypeJS.
$回调中的参数将引用 jQuery 对象,在该函数之外它可能引用另一个库,如 PrototypeJS。
回答by jAndy
$(document).ready(function() {});$(function() {});
$(document).ready(function() {});$(function() {});
The two statements are actually the exact same. So the second call is just a shortcut for the first.
这两种说法实际上是完全一样的。所以第二个调用只是第一个调用的快捷方式。
The $notation is again only a shortcut for jQuery. If you have loaded jQuery into your website you can use both. Especially if you don't load other JS librarys, which maybe also use the $sign. That brings us to your mentioned
该$符号再次只是 的快捷方式jQuery。如果您已将 jQuery 加载到您的网站,则可以同时使用两者。特别是如果您不加载其他 JS 库,这些库可能也使用该$标志。这让我们想到了你提到的
(function($){
}(jQuery));
call. What is done here is to make sure, that within your created function expression the $sign references to the jQueryobject. You're calling that anonymous function (which has $ as parameter) and pass the jQueryobject in.
称呼。这里所做的是确保在您创建的函数表达式中,$符号引用了jQuery对象。您正在调用该匿名函数(以 $ 作为参数)并将jQuery对象传入。
回答by Alejandro González
I encourage to read some articles that are very useful to understand somethings in jQuery ( and of course in javascript ), this articles explain how to create a jQuery plugin, but reading it you will understand some basic and important things, like closures witch is the meaning in this (function($){}(jQuery)); statement.
我鼓励阅读一些对理解 jQuery(当然还有 javascript)非常有用的文章,这篇文章解释了如何创建一个 jQuery 插件,但是阅读它你会理解一些基本和重要的东西,比如闭包女巫是这意味着 (function($){}(jQuery)); 陈述。
http://www.authenticsociety.com/blog/jQueryPluginTutorial_Beginner
http://www.authenticsociety.com/blog/jQueryPluginTutorial_Beginner

