Javascript jQuery $( function() {} ) 和 $(document).ready 一样吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10595913/
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
jQuery $( function() {} ) and $(document).ready the same?
提问by MegaNairda
To have a working datepicker on a field, I have to put this script inside my element
要在字段上使用有效的日期选择器,我必须将此脚本放在我的元素中
$( function() {
$( "#date_datepicker" ).datepicker( { dateFormat: "yy-mm-dd" } );
});
Removing the $( function() {
makes the datepicker not work.
删除 $( function() {
使日期选择器不起作用。
So does it mean that the $( function() {
is the same as $(document).ready
?
那么这是否意味着$( function() {
与相同$(document).ready
?
I'm trying to optimize my javascript codes so knowing this might help.
我正在尝试优化我的 javascript 代码,所以知道这可能会有所帮助。
回答by Frederick Behrends
See the extract below from http://api.jquery.com/ready/
请参阅以下摘录自http://api.jquery.com/ready/
All three of the following syntaxes are equivalent:
$(document).ready(handler)
$().ready(handler)
(this is not recommended)$(handler)
以下所有三种语法都是等效的:
$(document).ready(handler)
$().ready(handler)
(不推荐这样做)$(handler)
回答by Hans Wassink
The .ready() method is typically used with an anonymous function:
.ready() 方法通常与匿名函数一起使用:
$(document).ready(function() {
// Handler for .ready() called.
});
Which is equivalent to calling:
这相当于调用:
$(function() {
// Handler for .ready() called.
});
回答by Rich O'Kelly
Yes, it's a shorthand version of the same thing. The $
function calls the $(document).ready
function when passed a function as an argument.
是的,它是同一事物的简写版本。该$
函数$(document).ready
在将函数作为参数传递时调用该函数。
If you're trying to optimise in terms of speed - both will perform pretty much equivalently, however the longer $(document).ready(handler)
will be minimally faster if executed lots of times.
如果您尝试在速度方面进行优化 - 两者的性能几乎相同,但是$(document).ready(handler)
如果执行多次,时间越长速度越慢。
If you're trying to optimise in terms of file size - use a minifier.
如果您尝试在文件大小方面进行优化 - 使用压缩器。
IMO the best you can do is to 'optimise' in terms of readability and simplicity. This makes the code far easier to understand and maintain. There are tools out there to take an unoptimised version and compress and optimise for you (check out Google's closure compiler).
IMO 你能做的最好的事情就是在可读性和简单性方面“优化”。这使得代码更容易理解和维护。有一些工具可以获取未优化的版本并为您压缩和优化(查看 Google 的闭包编译器)。
回答by Prasenjit Kumar Nag
Yes, $( function() {
and $(document).ready
are same.
是的,$( function() {
而且$(document).ready
是一样的。
$( function() {
works as a shorthand syntax but $(document).ready
makes the code more readable.
$( function() {
用作速记语法,但$(document).ready
使代码更具可读性。
回答by Timo Huovinen
Here is a pretty safe way to run code on ready
这是一种在就绪时运行代码的非常安全的方法
jQuery(function($, undefined){
// code to run onready
});
Although I personally prefer doing it like this:
虽然我个人更喜欢这样做:
(function($){ // create scope and pass specific aliased variables
$(function($, undefined){ // attach callback to run onready
// code to run onready
});
})(jQuery);
This way you can make your own bundles of functionality without fear of breaking other peoples code or having your code broken by loose variable definitions. You can also call the variables that you pass along with whatever names that you want and have code that runs no on ready, for example.
通过这种方式,您可以制作自己的功能包,而不必担心破坏其他人的代码或因松散的变量定义而破坏您的代码。例如,您还可以调用您传递的变量以及您想要的任何名称,并让代码在就绪时运行。
(function($){ // create scope and pass specific aliased variables
$(document).on('click', 'a[href]', function(){
// code to run when a link is clicked
});
$(window).on('load',function(){
// code to run onload
});
$(function($, undefined){ // attach callback to run onready
// code to run onready
});
})(jQuery);
Note that these are the same
请注意,这些是相同的
$(document).bind('ready', function(){});
$(document).on('ready', function(){});
$(document).ready(function(){});
$(function(){});
And that document does not have a load event
并且该文档没有加载事件
$(document).on('load', function(){}); // will not work
回答by Jeroen
note that you can also find scripts like this:
请注意,您还可以找到这样的脚本:
jQuery(document).ready(function(){
here the $-sign is replace by jQuery to avoid conflicts with other library's
这里 $-sign 被 jQuery 替换,以避免与其他库的冲突
回答by Arun Dev
Are you using jQuerymobile? if so instead of using document ready use
你在使用 jQuerymobile 吗?如果是这样而不是使用文档就绪使用
$('#pageId').live('pageinit',function(){});