jQuery.ready() 中应该包含什么内容,哪些内容应该在 jQuery.ready() 之外?
jQuery.ready() 中应该包含什么内容,哪些内容应该在 jQuery.ready() 之外?
从性能角度来看,我在某处读到,将所有代码包装在 jQuery.ready()
中并不是一种有效的方法。
那么我的问题是:什么应该在里面,什么可以被排除而没有问题(我猜代表可以被留在外面,但这是一个快速猜测)?
谢谢
What should be in and what should be out of a jQuery.ready() ?
On a performance perspective, I've read somewhere that putting all the codes wrapped inside a jQuery.ready()
isn't an efficient way to go.
Then my question is : what should be in and what can be oustide without problems (I guess delegates could be kept outside but it's a fast guessing) ?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了获得最终性能,请将 js 放在结束 body 标记之前。这样你就可以完全消除 jquery 的准备。 UI 加载速度很快,因为它不会被下载的脚本和解析 js 时阻塞并调用您可以确保上面 dom 中的元素已准备好进行操作。
For ultimate performance put your js before the closing body tag. That way you can eliminate jquery ready altogether. The UI loads fast as it is not blocked by scripts being downloaded and when the js is parsed and invoked you can be sure the elements in the dom above are ready to be manipulated.
页面加载完成后调用 jQuery.ready()。请参阅 jQuery.ready() 中的第一句话。如果您希望在页面加载完成之前触发事件,则 jQuery.ready() 不是最佳选择。
jQuery.ready() is called after the page is done loading. See first sentence in jQuery.ready(). If you want events to fire before the page is done loading jQuery.ready() would not be the way to go.
您应该保留函数声明,例如
或任何其他声明。因为如果你把它全部塞进去,那么只有当整个 DOM 加载完毕后,该代码才会开始执行。否则,它会在下载后立即开始执行。仅当您的 javascript 需要下载 DOM 时,才应显示 jQuery 包装器。
You should keep function delcarations such as
or any other declaration. Because if you shove it all in, that code will start executing only when the whole DOM has loaded. Otherwise, it starts executing as soon as it downloaded. The jQuery wrapper should only be present when your javascript requires the DOM to be downloaded.