在什么情况下不需要jQuery的document.ready()? [英] Under what circumstances is jQuery's document.ready() not required?

查看:110
本文介绍了在什么情况下不需要jQuery的document.ready()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然约翰·雷西格(John Resig)的建议是正确地声明jquery.document.ready()函数中的所有事件,但我知道您实际上没有将所有内容放在其中.实际上,在某些情况下,将方法故意置于ready事件之外可能更为合适.

While John Resig's recommendation is, quite rightly, to declare all events within a jquery.document.ready() function, I know that you don't actually have to put everything in there. In fact, there are cases where it may be more appropriate to deliberately put methods outside of the ready event.

但是那些情况是什么?显然,最佳做法要求所有事件都在ready事件中声明,那么对于该事件之外的声明,最佳做法是什么?

But what are those cases? Obviously best practice dictates that all events are declared within the ready event, so what would best practice be for declarations outside that event?

修改 请记住,我不仅关注不太明显的内容,而且还希望对明显的部分也有一个很好的说明.例如,如果我在编写插件,则不会将该代码封装在jquery.document.ready()调用中.

Edit Remember, I'm not just after the less-obvious stuff, but I'd like to get a good account of the obvious parts as well. For example, if I'm writing a plugin, I wouldn't encapsulate that code in a jquery.document.ready() call.

推荐答案

将所有内容放入jquery.document.ready()中,

Put everything in jquery.document.ready() which:

  • 更改布局(您要防止页面闪烁)
  • 用户交互需要
  • 才能使页面可用

可以推迟的是:

  • 对于用户而言不重要的所有内容,例如分析,广告
  • (略有改善)用户体验的事物
  • 获取要添加到页面的不必要的小部件或按钮

这篇关于在什么情况下不需要jQuery的document.ready()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆