Javascript jQuery document.ready 与 pageLoad
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7824069/
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 vs pageLoad
提问by higgsy
I've picked up an existing project from another developer and ive noticed in the code that they are executing js code within three different event handlers...
我从另一个开发人员那里拿起了一个现有的项目,我在代码中注意到他们正在三个不同的事件处理程序中执行 js 代码......
function pageLoad() {
//execute code
}
$(document).ready(function() {
//execute code
});
$(function() {
//execute code
});
My question is - arent they all exactly the same? Or at least the last two? I understand that pageLoad is called by the .NET framework so it's not dependent on the jQuery library having loaded like the second two are - that's my understanding anyway - is that about correct?
我的问题是——它们是不是都完全一样?或者至少是最后两个?我知道 pageLoad 是由 .NET 框架调用的,因此它不依赖于像后两个那样加载的 jQuery 库 - 无论如何这是我的理解 - 这是否正确?
回答by Sunny
$(document).ready()
$(文档).ready()
Ideal for one time initialization.
Optimization black magic; may run slightly earlier than pageLoad().
Does not re-attach functionality to elements affected by partial postbacks.
非常适合一次性初始化。
优化黑魔法;可能会比 pageLoad() 稍早运行。
不会将功能重新附加到受部分回发影响的元素。
pageLoad()
页面加载()
Unsuitable for one time initialization if used with UpdatePanels.
Slightly less optimized in some browsers, but consistent.
Perfect for re-attaching functionality to elements within UpdatePanels.
如果与 UpdatePanels 一起使用,则不适用于一次性初始化。
在某些浏览器中优化稍差,但保持一致。
非常适合将功能重新附加到 UpdatePanels 中的元素。
回答by BNL
pageLoad
and the jQuery ready
handler are both methods of accomplishing similar things.
pageLoad
和 jQueryready
处理程序都是完成类似事情的方法。
The second two examples are identical.
后两个例子是相同的。
http://encosia.com/document-ready-and-pageload-are-not-the-same/
http://encosia.com/document-ready-and-pageload-are-not-the-same/
回答by Moin Zaman
The last one is just a shorthand notation of the one above it. http://www.jquery4u.com/dom-modification/types-document-ready/
最后一个只是它上面那个的速记符号。http://www.jquery4u.com/dom-modification/types-document-ready/
回答by Aman singh Parihar
$(document).ready(function() {
`enter code here`
//execute code
});
$(function() {
`enter code here`
});
Both functions perform task in the same way, executes code inside them once the document is ready.
这两个函数以相同的方式执行任务,一旦文档准备好就执行它们内部的代码。
function pageLoad() {
`enter code here`
}
This executes after every post back, generally used with update panels controls as the above two functions does not execute every time or every post back.
这在每次回发后执行,通常与更新面板控件一起使用,因为上述两个功能不是每次或每次回发都执行。
回答by Karan
$(document).ready()
will not fire for the partial postbacks (happened from AJAX). In that case, you should use MS AJAX
pageLoad function when you need to execute something when the page loads either from the full postback or partial.
$(document).ready()
不会触发部分回发(从 AJAX 发生)。在这种情况下,MS AJAX
当页面从完整回发或部分加载时需要执行某些操作时,您应该使用pageLoad 函数。
The article given in the Encosia site is a good read.
Encosia 站点中给出的文章很好读。
回答by javi_moralesf
In this site you will find the difference :)
在这个网站你会发现区别:)