可视化讲解 DOM 构建过程
前言
最近在看 Secrets of the JavaScript Ninja, 书中第二章讲到 DOM 的构建流程.
记得我之前也为理解 DOM 构建流程查阅过数次资料, 虽然每次查阅完都觉得 DOM 构建流程很简单, 看完便懂, 但是懂了又忘还是让人有些头疼.
为了给自己加深印象, 也为了为大家提供一个可视化的理解 DOM 构建过程的方式, 笔者制作了一个简单的网页来动态演示 DOM 构建过程. 希望能给大家带来一些帮助.
效果
在线查看
前进, 后退
网页展示了一个简单的 HTML 页面的 DOM 渲染过程. 用户点击前进,后退按钮时, 页面左侧会显示出当前的 HTML 代码, 右侧则会显示出实时的 DOM 结构图:
自动播放
点击 Auto Play
按钮, 页面会自动播放页面的整个构建过程:
DOM 构建过程
DOM 元素的作用 & 基本构建过程:
这里直接引用一下原文:
The goal of this page-building phase is to set up the UI of a web application, and this is done in two distinct steps:
1 Parsing the HTML and building the Document Object Model (DOM)
2 Executing JavaScript code
Step 1 is performed when the browser is processing HTML nodes, and step 2 is > performed whenever a special type of HTML element—the script element (that > contains or
refers to JavaScript code)—is encountered. During the page-building phase, the browser
can switch between these two steps as many times as necessary.
浏览器 页面构建 步骤的目的是为 UI 渲染做准备, 页面构建是由下面两部分购成的:
- 解析 HTML 节点, 并且构建 DOM 元素
- 执行 JavaScript 代码
其中第一步在浏览器解析到 HTML 节点时执行, 第二步在解析到