For example, the DOM sees elements
as node types, but an element is just one type of node. There are 12 different node
types, most of which are more relevant to the XML folks. For those who work with HTML,
there are only three types of nodes that are used regularly: elements, attributes, and text.
Table 2-2 shows the relevant node types.
Table 2-2. Node Types and the Corresponding Node Type IDs
Description Node Type
Element 1
Attribute 2
Text 3
Comment 8
Document 9
So that last diagram is a little inaccurate because I haven??™t represented my attribute or text
nodes. Figure 2-3 shows what the full DOM tree diagram should look like.
CHAPTER 2 n HTML, CSS, AND JAVASCRIPT 27
Figure 2-3. The DOM tree diagram, with the attribute and text nodes added
As you can see, that tree structure just got a few more branches. Keep in mind??”and this is
important??”that even the space between tags is represented by a text node.
IE, in its usual desire to be different, doesn??™t recognize that blank space as a node. When
you get to traversing the DOM, you??™ll need to keep this browser difference in mind (discussed
later in this chapter).
The document Object
Now that you know what the DOM is, let??™s have a look at how you use it (using the document
object, of course). From the document object you can reference every element on the page, add
new elements, and remove existing elements.
When working with the document, there are a few functions to get one or more elements,
three of which are most common:
??? getElementById(): Retrieves a single element from the page.
Pages:
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73