The Document Object Model (DOM) is an application that represents various elements of a document (such as HTML and XML) and accesses and operations.
Program API ). Generally, all browsers that support JavaScript support Dom. The Dom involved in this article refers to the standard Document Object Model defined by W3C. It represents HTML and XML documents in a tree structure, defines the methods and attributes for traversing the tree and checking and modifying the node of the tree.
HTML document in Dom eye: Tree
In the eyes of Dom, HTML is a tree-like document like XML. <HTML> it is a root node, Is this a bit similar to the XML structure. The difference is that the tree structure of HTML documents mainly contains nodes that represent elements, tags, and text strings.
Note: In Firefox, you can use the menu "tool"-> "dom viewer" to browse the DOM structure of the document. In IE browser, You need to download microsoft's inte. net explorer developer toolbar to view the Dom, in java-cn.org/edit.jsp? Page = browserdevtool "> browserdevtool download this tool.
HTML document Node
in Dom, each node in the HTML document is considered as a node object of various types. Each node object has its own attributes and methods. These attributes and methods can be used to traverse the entire document tree. Due to the complexity of HTML documents, Dom defines nodetype to represent the node type. The following lists the common node types of node:
Note of the nodetype constant nodetype value of the interface
element node. element_node 1 element node
text node. text_node 3 text node
document node. document_node 9 document
comment node. comment_node 8 annotated text
documentfragment node. document_fragment_node 11 document segment
ATTR node. attribute_node 2 node attribute
The root node of the DOM tree is a document object. The documentelement attribute reference of this object indicates the Element Object of the document root element (for HTML documents, this is the <HTML> mark ). When Javascript is used to operate an HTML document, the document points to the entire document, and the node types such as <body> and <Table> are element. Comment nodes refer to comments of documents. For more information about the node types, see the Javascript authoritative guide.
Most of the methods defined by document are production methods, which are mainly used to create various types of nodes that can be inserted into documents. Common Document methods include:
Method description
Createattribute () creates a new ATTR node with the specified name.
Createcomment () creates a new comment node with the specified string.
Createelement () creates a new element node with the specified tag name.
Createtextnode () creates a textnode with the specified text.
Getelementbyid () returns the element node with the specified ID attribute in the document.
Getelementsbytagname () returns all element nodes with the specified tag name in the document.
For element nodes, you can call the getattribute (), setattribute (), and removeattribute () Methods to query, set, or delete the nature of an element node, such as the border attribute marked by <Table>. Common attributes of an element are listed below:
The tag name of the tagname element. For example, the <p> element is P. Tabname returned by the HTML document is in uppercase.
Common Methods of element:
Method description
Getattribute () returns the value of a specified attribute in the form of a string.
Getattributenode () returns the value of the specified attribute in the form of an ATTR node.
Getelementsbytabname () returns a node array that contains the child nodes of all the element nodes with the specified tag name. The sequence is the order in which the object appears.
Hasattribute () returns true if the element has an attribute of the specified name.
Removeattribute () deletes a specified attribute from an element.
Removeattributenode () deletes the specified ATTR node from the attribute list of the element.
Setattribute () sets the specified attribute to the specified string value. If the attribute does not exist, a new attribute is added.
Setattributenode () adds the specified ATTR node to the attribute list of the element.
The ATTR object represents the attributes of the document element, including the name and value Attributes. It can be obtained through the attributes attribute of the node interface or the getattributenode () method of the element interface. However, in most cases, the simplest method to use element attributes is getattribute () and setattribute (), rather than the ATTR object.
Use Dom to operate HTML documents
The Node object defines a series of attributes and methods to facilitate traversing the entire document. You can use the parentnode attribute and the childnodes array to move up and down the document tree. You can traverse the childnodes array or use the firstchild and nextsibling attributes for loop operations. You can also use lastchild and previussibling for reverse loop operations, you can also enumerate child nodes of a specified node. The appendchild (), insertbefore (), removechild (), and replaceChild () methods can be called to change the child nodes of a node and thus change the document tree.
It should be noted that the value of childnodes is actually a nodelist object. Therefore, you can enumerate all subnodes of a given node by traversing each element of the childnodes array. By recursion, You can enumerate all nodes in the tree. The following table lists some common attributes and methods of a Node object:
Common attributes of node objects:
Attribute description
Attributes if the node is an element, the attribute of the element is returned in the form of namednodemap.
Childnodes stores the subnodes of the current node as nodes. If no child node exists, an empty array is returned.
Firstchild returns the first child node of the current node as a node. If no subnode exists, the value is null.
Lastchild returns the last child node of the current node as a node. If no subnode exists, the value is null.
Nextsibling returns the sibling node of the current node as a node. If no such node exists, null is returned.
The name of the nodename node. The element node represents the Tag Name of the element.
Nodetype indicates the node type.
Parentnode returns the parent node of the current node as a node. If no parent node exists, the value is null.
Previussibling returns the sibling node next to the current node and before it as a node. If no such node exists, null is returned.
Common Methods for node objects:
Method description
Appendchild () adds a node to the childnodes Group of the current node to the document tree.
Clonenode () copies the current node, or copies the current node and all its child nodes.
Haschildnodes () returns true if the current node has a subnode.
Insertbefore () inserts a node into the document tree, which is located before the specified child node of the current node. If the node already exists, delete it and insert it to its location.
Removechild () is deleted from the document tree and the specified child node is returned.
ReplaceChild () is deleted from the document tree and the specified child node is returned. replace it with another node.
Next, let's use the preceding Dom Application Programming Interface to try to operate HTML documents.
The simplest example is to use Dom to dynamically generate a loading IMG and add it to the page. After the page is fully loaded, the IMG object will be removed.
<SCRIPT> // create a loading IMG and add to pagevar loadingimg = document. createelement ("IMG"); loadingimg. src = "images/loading_xp.gif"; document. body. appendchild (loadingimg); // after page loaded, we remove the loading imagedocument. body. onload = function () {document. body. removechild (loadingimg) ;}; </SCRIPT>
Ajax_follow_me3_xml_and_dom-<a class = tigfont onclick = "displaytip (=" "Java =" ") ="> parse "> parse and generate xml in Javascript
The domdocument object is generally used to parse and generate xml using Js. The following describes its attributes and methods:
Attribute:
Attribute list of the attributes storage node (read-only)
List of child nodes of the childnodes storage node (read-only)
Datatype returns the Data Type of this node
Definition defines nodes in DTD or XML mode (read-only)
Doctype specifies the file type node (read-only)
Documentelement returns the root element of the document (read/write)
Firstchild returns the first child node of the current node (read-only)
Implementation returns the xmldomimplementation object
Lastchild returns the last child node of the current node (read-only)
Nextsibling returns the next sibling node of the current node (read-only)
Nodename: return the node name (read-only)
Nodetype return node type (read-only)
Nodetypedvalue stores node values (read/write)
Nodevalue returns the node text (read/write)
Ownerdocument returns the root document containing this node (read-only)
Parentnode returns the parent node (read-only)
Parsed: returns whether the node and Its subnodes have been parsed (read-only)
Prefix returns the namespace prefix (read-only)
Preservewhitespace specifies whether to retain white space (read/write)
Previussibling returns the previous sibling node of this node (read-only)
Text returns the text content of the node and its descendants (read/write)
URL returns the URL of the recently loaded XML document (read-only)
XML returns the XML Representation of the node and its descendants (read-only)
Method:
Appendchild adds a new subnode to the current node, which is placed after the last subnode
Clonenode returns the copy of the current node
Createattribute
Createcdatasection create CDATA segments that contain the given data
Createcomment create a comment Node
Createdocumentfragment create a documentfragment object
Createelement: Create an element node
Createentityreference: Create an entityreference object
Createnode creates nodes of the given type, name, and namespace.
Createporcessinginstruction
Createtextnode: Create a text node that includes the given data
Getelementsbytagname returns the set of elements with the specified name.
Haschildnodes: returns whether the current node has subnodes.
Insertbefore inserts a subnode before a specified Node
Load import the XML document at the specified position
Loadxml import the XML document of the specified string
Removechild deletes a specified subnode from the subnode list.
ReplaceChild replaces the specified child node from the child node list
Save the XML file to the specified Node
Selectnodes matches the specified node and returns the list of matched nodes.
Selectsinglenode matches the specified node and returns the first matching node.
Transformnode converts nodes and their descendants using the specified style table.
Transformnodetoobject: Use the specified style sheet to convert nodes and their descendants to objects.