The first thing to understand is that the DOM is a tree based API for XML, which has many implementations (each language has its own implementation), and we discuss the implementation of the DOM in JavaScript or XHTML (HTML).
First, using the DOM
Consider an HTML file:
<body>
<p>测试</p>
</body>
1. Access nodes:
Accessing HTML elements: var ohtml=document.documentelement;
element: var ohead=ohtml.firstchild;
Get the BODY element: Var obody=ohtml.lastchild; or Var obody=document.body;
You can also do the same thing through ChildNodes:
var ohead=ohtml.childnodes[0] or OHtml.childNodes.item (0);
var obody=ohtml.childnodes[1] or OHtml.childNodes.item (1);
To determine the relationship between nodes:
alert(oHead.parentNode==oHtml);
alert(oBody.previousSibling==oHead);
alert(oHead.nextSibling==oBody);
alert(oHead.ownerDocument==document);
2. Detection node Type:
The node type is validated by the NodeType property of the node:
alert (Document.nodetype); Output 9
It should be noted that DOM-compatible browsers (take FF2.0 as an example) have constants such as Node.document_node, Node.element_node, and so on. The constant names and numerical tables are as follows:
ELEMENT_NODE 1
ATTRIBUTE_NODE 2
TEXT_NODE 3
CDATA_SECTION_NODE 4
ENTITY_REFERENCE_NODE 5
ENTITY_NODE 6
PROCESSING_INSTRCTION_NODE 7
COMMENT_NODE 8
DOCUMENT_NODE 9
DOCUMENT_TYPE_NODE 10
DOCUMENT_FRAGMENT_NODE 11
NOTATION_NODE 12
IE6 not supported, but you can customize a JS object node.