(4)事件处理——(3)代码执行的顺序(Timing of code execution)
2013-10-10 11:31
441 查看
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处理器),这两种机制可以和平共处。
在第一章,入门指南中,我们说明了$(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)
- 应用程序按照以下顺序执行由 global.asax 文件中定义的模块或用户代码处理的事件
- Execution Order of Event Functions, unity 3d 事件函数的执行顺序
- 关于Form KeyDown事件的处理代码执行两次的问题
- (4)事件处理——(5)为代码简洁做的快捷方式(Shortcuts for code brevity)
- (4)事件处理——(5)为代码简洁做的快捷方式(Shortcuts for code brevity)
- 处理PHP mysql_query执行超时 【Fatal error: Maximum execution time of 300 seconds exceeded in……】
- 同一事件绑定不同的处理方法、执行顺序有所不同
- Salesforce事务执行顺序(Triggers and Order of Execution)
- 09 java的代码的执行顺序和异常处理
- asp.net页面事件执行顺序(转+原创补充) Button1_Click处理返回数据前还有其他处理!!!例如onPreLoad
- SQL Server安全(6/11):执行上下文与代码签名(Execution Context and Code Signing)
- 在执行一行代码之前CLR做的68件事[The 68 things the CLR does before executing a single line of your code]
- new 和 alloc init 的区别 和视图控制器创建到显示到屏幕上的代码执行顺序
- aspx页面事件执行顺序
- libpng: Remote execution of arbitrary code
- ASP.net中页面事件的先后执行顺序(引用)
- Page 执行中将按照如下顺序激活事件
- 从join on和where执行顺序认识T-SQL查询处理执行顺序
- ASP.NET 2.0中Page事件的执行顺序