jQuery 文档就绪函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5754192/
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 document ready function
提问by tmbrggmn
Are the end results of the following jQuery snippets identical?
以下 jQuery 片段的最终结果是否相同?
Snippet 1:
片段 1:
$(function() { alert('test!'); });
Snippet 2:
片段 2:
$(document).ready(function() { alert('test!'); });
In other words, is $(function(){})
just shorthand for $(document).ready(function() { });
?
换句话说,$(function(){})
只是$(document).ready(function() { });
?
The reason I'm asking is because we're seeing some strange issues with a small application we've built using jQuery and jQuery UI. Occasionally, when performing a form submit action by clicking a button the browser window will just freeze. We can still use the underlying browser window (the one that launches the pop-up) until we perform some actions there. The users can only continue by force closing the browser (Internet Explorer, obviously). We suspect this is related to the Acrobat PDF plug-in, but I'm just checking all the angles here, because I found this issuewhich seems to exhibit similar behaviour.
我问的原因是因为我们发现使用 jQuery 和 jQuery UI 构建的小应用程序存在一些奇怪的问题。有时,当通过单击按钮执行表单提交操作时,浏览器窗口会冻结。我们仍然可以使用底层浏览器窗口(启动弹出窗口的窗口),直到我们在那里执行一些操作。用户只能通过强制关闭浏览器(显然是 Internet Explorer)来继续。我们怀疑这与 Acrobat PDF 插件有关,但我只是在这里检查所有角度,因为我发现这个问题似乎表现出类似的行为。
采纳答案by dmarucco
回答by Andrei Andrushkevich
All three of the following syntaxes are equivalent:
以下所有三种语法都是等效的:
$(document).ready(handler)
$().ready(handler) (this is not recommended)
$(handler)
Aliasing the jQuery Namespace
jQuery 命名空间的别名
When using another JavaScript library, we may wish to call $.noConflict() to avoid namespace difficulties. When this function is called, the $ shortcut is no longer available, forcing us to write jQuery each time we would normally write $. However, the handler passed to the .ready() method can take an argument, which is passed the global jQuery object. This means we can rename the object within the context of our .ready() handler without affecting other code:
当使用另一个 JavaScript 库时,我们可能希望调用 $.noConflict() 以避免命名空间困难。调用此函数时,$ 快捷方式不再可用,迫使我们每次通常编写 $ 时都要编写 jQuery。但是,传递给 .ready() 方法的处理程序可以接受一个参数,该参数传递给全局 jQuery 对象。这意味着我们可以在 .ready() 处理程序的上下文中重命名对象,而不会影响其他代码:
jQuery(document).ready(function($) {
// Code using $ as usual goes here.
});
回答by Jatin Ganhotra
$(function(){})
and $(document).ready(function() { })
are identical.
$(function(){})
并且$(document).ready(function() { })
是相同的。
回答by Imran Khan
Yes, this is identical. but the first one is usually used in jquery for easiness.
是的,这是相同的。但是为了方便起见,第一个通常在 jquery 中使用。
$(function() {
alert('test!');
});
回答by Imran Khan
The following code also working
以下代码也有效
$(document).ready(function(){
alert("success");
});
OR
或者
$(function(){
alert("succes");
});
回答by 2540625
Yes:
是的:
$(document).ready(function() {
/* code */
});
…and:
…和:
$(function() {
/* code */
});
…are effectively the same, and the latter is commonly referred to as shorthand for the former.
...实际上是相同的,后者通常被称为前者的简写。
If you're wondering whythey produce the same outcome, it has to do with the jQuery constructor—the jQuery()
function, aliased as $()
—and its allowed inputs.
如果你想知道为什么他们产生相同的结果,它与jQuery的构造做-thejQuery()
功能,别名为$()
-和其允许的输入。
The constructor is documented at api.jquery.com/jquery/, and its two relevant options are outlined below.
构造函数记录在api.jquery.com/jquery/,其两个相关选项概述如下。
jQuery( selector [, context ] )
Accepts a string containing a CSS selector which is then used to match a set of elements.
Returns a jQuery object.
jQuery( selector [, context ] )
接受一个包含 CSS 选择器的字符串,然后用于匹配一组元素。
返回一个 jQuery 对象。
This option above is how you're invoking the jQuery constructor when writing:
上面的这个选项是你在编写时调用 jQuery 构造函数的方式:
$(document).ready(function() { /* code */ });
The document
object is selected and used to construct a jQuery object. When the DOM is fully loaded, that jQuery object invokes the callback (the anonymous function) within ready()
.
该document
对象被选中并用于构造一个jQuery 对象。当 DOM 完全加载时,该 jQuery 对象会调用ready()
.
jQuery( callback )
Binds a function to be executed when the DOM has finished loading.
Returns a jQuery object.
jQuery( callback )
绑定一个函数,当 DOM 完成加载时执行。
返回一个 jQuery 对象。
This option above is how you're invoking the jQuery constructor when writing:
上面的这个选项是你在编写时调用 jQuery 构造函数的方式:
$(function() { /* code */ });
The callback function (the anonymous function) is used to construct a jQuery object, and when the DOM is fully loaded, it is invoked.
回调函数(匿名函数)用于构造一个 jQuery 对象,并在 DOM 完全加载时调用它。
回答by Sk Sharma
Different ways to write jQuery Document Ready Snippet
编写 jQuery Document Ready Snippet 的不同方法
$(document).ready(function() { ... });
$(function() { ... });
jQuery(document).ready(function() { ... });
jQuery(function() { ... });
http://webiwip.com/interview-questions-answers/jquery-interview-questions/10510
http://webiwip.com/interview-questions-answers/jquery-interview-questions/10510