DOM Nodes

  1. An idea of DOM

    1. Another document
  2. Walking DOM using Developer Tools
  3. Whitespace nodes
  4. Other node types
    1. DOCTYPE
    2. Comments
  5. Summary

The DOM represents a document as a tree. The tree is made up of parent-child relationships, a parent can have one or many children nodes.

An idea of DOM

Let’s consider the following HTML as the starting point:

1 <html>
2   <head>
3     <title>The title</title>
4   </head>
5   <body>
6      The body
7    </body>
8 </html>

The DOM will look like that:

At the top level, there is an html node, with two children: head and body, among which only head has a child tag title.

HTML tags are element nodes in DOM tree, pieces of text become text nodes. Both of them are nodes, just the type is different.

•The idea of DOM is that every node is an object. We can get a node and change its properties, like this:

1 // change background of the <BODY> element, make all red
2 document.body.style.backgroundColor = ‘red‘;

If you have run the example above, here is the code to reset the style to default:

1 // revert background of <BODY> to default, put it back
2 document.body.style.backgroundColor = ‘‘;

•It is also possible to change the contents of nodes, search the DOM for certain nodes, create new elements and insert them into the document on-the-fly.

But first of all we need to know what DOM looks like and what it contains.

Another document

Let’s see the DOM of a more complicated document.

01 <!DOCTYPE HTML>
02 <html>
03     <head>
04         <title>The document</title>
05     </head>
06     <body>
07         <div>Data</div>
08         <ul>
09             <li>Warning</li>
10             <li></li>
11         </ul>
12         <div>Top Secret!</div>
13     </body>
14 </html>

And here is the DOM if we represent it as a tree.

Walking DOM using Developer Tools

It is quite easy to walk DOM using a browser developer tool.
For example:

  1. Open simpledom2.html
  2. Open Firebug, the Chrome & IE built in developer tools or any other developer tool
  3. Go HTML tab in Firebug & IE devtools or Elements tab in Safari/Chrome or.. whatever.
  4. Here you are.

Below is a screenshot from Firebug for the document above. Basically it’s layout is same as HTML, plus the minus icon [-] for nodes.

The DOM displayed in developer tools is not complete. There are elements, which exist in DOM, that are not shown in developer tools.

Whitespace nodes

Now let’s make the picture closer to reality and introduce whitespace text elements. Whitespace symbols in the HTML are recognized as the text and become text nodes. These whitespace nodes are not shown in developer tools, but they do exist.

The following picture shows text nodes containing whitespaces.

By the way, note that last li does not have a whitespace text tag inside. That’s exactly because there is no text at all inside it.

Whitespace nodes are created from spaces between nodes. So they disappear if we elimitate the space between tags.

The example below has no whitespace nodes at all.

<!DOCTYPE HTML><html><head><title>Title</title></head><body></body></html>

IE<9

Versions of IE lower than 9 differ from other browsers because they do not generate tags from whitespaces. This was corrected in IE 9 as it complies with standards.

But in older IEs, the DOM tree is different from other browsers because of this.

Other node types

DOCTYPE

Did you notice a DOCTYPE from the example above? That is not shown on the picture, but DOCTYPE is a DOM node too, located at the topmost level to the left from HTML.

DocType is part of the HTML specification and it is an instruction to the web browser about what version of the markup language the page is written in.

Comments

Also, a HTML comment is a DOM node too:

1 <html>
2 ...
3 <!-- comment -->
4 ...
5 </html>

The comment above is also stored in DOM tree, with comment node type and textual content. It is important when traversing nodes as we’ll see.

Summary

Now you should have understanding of DOM structure, how it looks like, which nodes it includes.

The next chapter describes how to traverse it using JavaScript.

时间: 2024-10-06 01:21:41

DOM Nodes的相关文章

HTML DOM Nodes

HTML DOM Nodes 介绍 HTML DOM将HTML文档视为节点树.HTML DOM Nodes分为:文档节点.元素节点.属性节点.文本节点.注释节点. HTML DOM Nodes 层级 节点树的层级关系分为:父parent.子child.同胞sibling.    HTML DOM Nodes 方法 HTML DOM 方法是可以在节点上执行的动作.元素节点: 查询元素节点 var element = document.getElementById('id'); var elemen

[HTML5] DOM Nodes Explained

import './assets/css/style.css'; const app = document.getElementById('app'); app.innerHTML = '<h1>JavaScript DOM</h1>'; // <html> console.log(document.documentElement); console.dir(document.documentElement); // <head> console.dir(d

DOJO DOM 功能

In this tutorial, you'll learn about how to use Dojo to manipulate the DOM in a simple, cross-browser way. Using basic DOM knowledge and only a few Dojo functions, you will be able to efficiently create, read, update and delete elements in the page o

Vue源码探究-虚拟DOM的渲染

Vue源码探究-虚拟DOM的渲染 在虚拟节点的实现一篇中,除了知道了 VNode 类的实现之外,还简要地整理了一下DOM渲染的路径.在这一篇中,主要来分析一下两条路径的具体实现代码. 按照创建 Vue 实例后的一般执行流程,首先来看看实例初始化时对渲染模块的初始处理.这也是开始 mount 路径的前一步.初始包括两部分,一是向 Vue 类原型对象上挂载渲染相关的方法,而是初始化渲染相关的属性. 渲染的初始化 下面代码位于vue/src/core/instance/render.js 相关属性初始

JavaScript 的 API 设计原则

前言 本篇博文来自一次公司内部的前端分享,从多个方面讨论了在设计接口时的原则,总共包含了七个大块.系卤煮自己总结的一些经验教训.同时也参考了一些文章,地址会在后面贴出来.很难做到详尽充实,如果有好的建议或者不对的地方,还望不吝赐教斧正. 一.接口的流畅性 好的接口是流畅易懂的,他主要体现如下几个方面: 1.简单 操作某个元素的css属性,下面是原生的方法: document.querySelectorAll('#id').style.color = 'red'; 封装之后 function a(

读jQuery源码之三

源码177-527行:jQuery.extend方法 jQuery.extend = jQuery.fn.extend = function() { var options, name, src, copy, copyIsArray, clone, target = arguments[0] || {}, i = 1, length = arguments.length, deep = false; // Handle a deep copy situation if ( typeof targ

jQuery源码

/*! * jQuery JavaScript Library v1.8.3 * http://jquery.com/ * * Includes Sizzle.js * http://sizzlejs.com/ * * Copyright 2012 jQuery Foundation and other contributors * Released under the MIT license * http://jquery.org/license * * Date: Tue Nov 13 20

JQuery data API实现代码分析

JQuery data 接口是什么? .data() Store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements. 根据jquery官网介绍,data给存储DOM关联的数据, 设置数据是对$选取的所有JQuery对象, 获取数据是对$选取的所有对象的

jquery 2.1.0 源码

/*! * jQuery JavaScript Library v2.1.0 * http://jquery.com/ * * Includes Sizzle.js * http://sizzlejs.com/ * * Copyright 2005, 2014 jQuery Foundation, Inc. and other contributors * Released under the MIT license * http://jquery.org/license * * Date: 2