In Chapter 1, Getting Started, we noted that $(document).ready()was jQuery's primary way to perform tasks on page load. It is not, however, the only method at our disposal. The native window.onloadevent can achieve a similar effect. While the two methods are similar, it is important to recognize their difference in timing, even though it can be quite subtle depending on the number of resources being loaded.

在第一章,入门指南中,我们说明了$(document).ready()是jquery用来在页面加载的时候执行任务的私有的方法。然而,他不是我们可用的唯一的方法。原生的window.onload事件可以实现相似的影响。随涨这两个方法是相似的,但是辨别出他们在执行是的不同是十分重要的,尽管他们可能只是在加载资源的数量上有着微妙的不同。

The window.onloadevent fires when a document is completely downloaded to the browser. This means that every element on the page is ready to be manipulated by JavaScript, which is a boon for writing featureful code without worrying about load order.

window.onload事件在文档完全被加载到浏览器的时候执行。这意味着网页上每一个元素已经可以被js操纵了。这对我们写有特色的代码而不用担心加载顺序这一点是一个福利。

On the other hand, a handler registered using $(document).ready()is invoked when the DOM is completely ready for use. This also means that all elements are accessible by our scripts, but does not mean that every associated file has been downloaded. As soon as the HTML has been downloaded and parsed into a DOM tree, the code can run.

另一方面,使用$(document).ready()注册的方法在DOM结构完全可以使用的时候被调用。这也意味着所有的元素可以通过我们的脚本来接触到,但是这不意味着所有相关的文件已经被下载下来了。一旦HTML已经被加载而且被解析成DOM树的时候,这段代码就可以执行了。

Style loading and code execution
To ensure that the page has also been styled before the JavaScript code executes, it is a good practice to place <link rel="stylesheet">and <style>tags prior to <script>tags within the document's <head>element.

样式加载和代码执行
为了确保在js代码执行之前已经加载了样式,在文档的head元素里面把 <script>标签放到<style>和<link rel="stylesheet">之前是一个很好的实践。

Consider, for example, a page that presents an image gallery; such a page may have many large images on it, which we can hide, show, move, and otherwise manipulate with jQuery. If we set up our interface using the onloadevent, users will have to wait until each and every image is completely downloaded before they can use those features. Even worse, if behaviors are not yet attached to elements that have default behaviors (such as links), user interactions could produce unintended outcomes. However, when we use $(document).ready()for the setup, the interface is ready to use earlier with the correct behavior.

比如,假设一个网页需要呈现一个图片库,这样的网页可能有很多很大的网页,我们可能隐藏移动或者使用jquery操控他们。如果我们使用onload事件去启动我们的界面,用户可能需要等到所有的图片被完全加载以后才可能使用这些特性。更糟糕的是,如果行为还没有绑定到拥有默认行为的元素上的时候(比如links),用户的反应可能会造成我们本不想要的输出。然而,当我们使用$(document).ready()来启动页面,页面会更早的拥有正确的行为。

What is loaded and what is not?
Using $(document).ready()is almost always preferable to using an onloadhandler, but we need to keep in mind that because supporting files may not have loaded, attributes such as image height and width are not necessarily available at this time. If these are needed, we may, at times, also choose to implement an onloadhandler (or more likely, use jQuery to bind a handler to the loadevent); the two mechanisms can coexist peacefully.

什么被加载了,什么没有被加载
使用$(document).ready()几乎总是比使用onload处理器要好,但是我们需要牢牢记住由于支持文件可能还没有被加载,类似于图片高度和宽度的属性在这是可能是不可用的。如果我们需要这些属性,有时候,我们可能需要选择实现onload处理器(或者更好的是,使用jquery去绑定一个onload处理器),这两种机制可以和平共处。

(4)事件处理——(3)代码执行的顺序(Timing of code execution)的更多相关文章

  1. jsp中java代码、jsp代码、js代码执行的顺序

    原理: jsp中的Java代码 -- 服务器端代码 js代码 -- 客户端代码 java是在服务器端运行的代码,jsp在服务器的servlet里运行,而JavaScript和html都是在浏览器端运行 ...

  2. 使用定时器判断确保某个标签有值才执行方法, 控制js代码执行先后顺序

    使用定时器判断确保某个标签有值才执行方法: var wait = setInterval(function(){ var diqu = $("#diqu").val(); //确保 ...

  3. github上拉去代码执行 npm install报错code:128

    npm ERR! code npm ERR! Command failed: D:\Program Files\Git\cmd\git.EXE clone --mirror -q git://gith ...

  4. mysql之代码执行结构

    本文内容: 什么是代码执行结构 顺序结构 分支结构 循环结构 首发日期:2018-04-18 什么是代码执行结构: 这里所说的代码执行结构就是多条sql语句的执行顺序. 代码执行结构主要用于触发器.存 ...

  5. PHP代码执行流程

    怎么样?有点了解了么.说实话,单看这个,我本人是有点懵的,不过,不要怕.咱们来慢慢地看下. 首先,在网上找的信息说PHP代码执行的顺序是这样的,第一步是词法分析,第二步是语法分析,第三步是转化为opc ...

  6. Java代码执行顺序(静态变量,非静态变量,静态代码块,代码块,构造函数)加载顺序

    //据说这是一道阿里巴巴面试题,先以这道题为例分析下 public class Text { public static int k = 0; public static Text t1 = new ...

  7. 当C#中带有return的TryCatch代码遇到Finally时代码执行顺序

    编写的代码最怕出现的情况是运行中有错误出现,但是无法定位错误代码位置.综合<C#4.0图解教程>,总结如下: TryCatchFinally用到的最多的是TryCatch,Catch可以把 ...

  8. js代码执行顺序问题

      前  言 LiuDaP 今天就给大家介绍一个特别基础的东西,javascript中函数的一点儿小知识(js代码的执行顺序),希望对大家有那么一点点帮助吧!!! 一.js--->单线程 严格意 ...

  9. final、static、代码块、静态代码块、内部类、代码执行顺序

    final final域使得确保初始化安全性(initialization safety)成为可能,初始化安全性让不可变形对象不需要同步就能自由地被访问和共享 作用在类上               ...

随机推荐

  1. UVA 103 Stacking Boxes 套箱子 DAG最长路 dp记忆化搜索

    题意:给出几个多维的箱子,如果箱子的每一边都小于另一个箱子的对应边,那就称这个箱子小于另一个箱子,然后要求能够套出的最多的箱子. 要注意的是关系图的构建,对箱子的边排序,如果分别都小于另一个箱子就说明 ...

  2. 开发汉澳即时通信网,2006年上线,QQ死期到了

    为汉澳sinox用户打造即时通信网让大家用上即时通信软件 近期腾讯关闭了linuxQQ登录,汉澳 sinox也登陆不上.非windows用户再也不能用上即时通信软件了! 这是多么可悲的事,可是我们必须 ...

  3. DQL

    DQL(Data QueryLanguage) 基本格式 select * from 表名 对于列进行限制 格式一:取指定列 select 列1,列2 from 表名 格式二:为列起别名的三种表示法, ...

  4. postgreSQL远程连接出现:Error connecting to server :致命错误 SSL关闭的pg_hba.conf记录

    异常截图:

  5. ES6笔记③

    1.查找关键字  includes(); 返回布尔值 //①:includes -->代替-->indexof-->返回布尔值 var str = "769909303&q ...

  6. eclipse中安装svn插件实现版本控制

    打开eclipse -> Help ->Install New Software选项, 点击Add按钮 根据需要,添加自己需要的版本svn控制器的版本,填写name和url,点击ok. N ...

  7. [置顶] boost使用(六)

    今天继续带来boost关于线程锁的一些使用,今天我使用一个更复杂的锁来控制同步,先来段简单的代码 #include <boost/thread.hpp> #include <iost ...

  8. 浮动层固定兼容IE6 position:fixed的最佳解决方案

    第一种:css方法 有时候当我们需要把一个元素固定在页面的某个部位,一般都是用css中的“position:fixed;”方法来解决,但是IE6不支持fixed,所以今天分享一个兼容IE6的页面底部固 ...

  9. Android studio 查看sha1

    高德地图开发申请KEY的时候需要开发者提供SHA1证书指纹数据,在eclipse很容易就找到了,但是Android Studio很久也没找到,只能使用在网上看到的方法了,在Android Studio ...

  10. Android 开发中常用小技巧

    TextView中的getTextSize返回值是以像素(px)为单位的, 而setTextSize()是以sp为单位的. 所以如果直接用返回的值来设置会出错,解决办法是 用setTextSize() ...