JavaScript Dom Technology (i)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.