问题描述
我从另一个开发人员那里接过了一个现有项目,我在代码中注意到我在三个不同的事件处理程序中执行js代码...
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...
function pageLoad() {
//execute code
}
$(document).ready(function() {
//execute code
});
$(function() {
//execute code
});
我的问题是-他们都完全一样吗?或至少是最后两个?我知道pageLoad是由.NET框架调用的,因此它不依赖于已加载的jQuery库,就像后两者一样-这还是我的理解-有关吗?
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?
推荐答案
$(document).ready()
-
理想的一次初始化.
Ideal for one time initialization.
优化黑魔法;可能比pageLoad()稍早运行.
Optimization black magic; may run slightly earlier than pageLoad().
不将功能重新附加到受部分回发影响的元素上.
Does not re-attach functionality to elements affected by partial postbacks.
pageLoad()
-
如果与UpdatePanels一起使用,则不适合一次初始化.
Unsuitable for one time initialization if used with UpdatePanels.
在某些浏览器中优化程度略低,但保持一致.
Slightly less optimized in some browsers, but consistent.
完美地将功能重新附加到UpdatePanels中的元素.
Perfect for re-attaching functionality to elements within UpdatePanels.
这篇关于jQuery document.ready与pageLoad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!