Js DOM model operations

Source: Internet
Author: User

Nodes in the DOM model: Element nodes, text nodes, and attribute nodes
Example: <a href = "http://www.cnblogs.com/shuz”> private comment dotnet hut </a>
(1) a is an element node.
(2) "の dotnet hut" is a text node
(3) href = "http://www.cnblogs.com/shuz?is the worker Node
DOM node attributes

Attribute

Type

Description

NodeName

String

The node name, which is defined based on the node type.

NodeValue

String

Node value, defined based on the node type

NodeType

Number

Node type. 1 indicates an element node, 2 indicates an attribute node, and 3 indicates a text node.

FirstChild

Node

Point to the first node in the childNodes list

LastChild

Node

Point to the last node in the childNodes list

ChildNodes

NodeList

List of all child nodes. childNodes [I] can access node I + 1.

ParentNode

Node

Point to the parent node of the node. If it is already the root node, null is returned.

Previussibling

Node

Refers to the forward sibling node. If it is already the first node, null is returned.

NextSibling

Node

Point to the next sibling node. If it is already the last node, null is returned.

Attributes

NameNodeMap

An Attr object that contains an element feature. It is used only for element nodes.

ClassName

String

Node CSS class

InnerHTML

String

All content between a tag, including the Code itself

DOM Node Method
(1) Access Node:
[By tag name]
Document. getElementsByTagName (sTagName) method: returns a list of element nodes containing the same tag name.
[By tag id]
Document. getElementById (sElementId) method: returns the element node whose Id is the specified value.
[Access the previous node] compatible with IE and FireFox
(Custom)
Copy codeThe Code is as follows:
Function prevSib (oNode ){
Var oTempFirstNode = oNode. parentNode. firstChild;
// Determine whether the node is the first node. If yes, null is returned.
If (oNode = oTempFirstNode)
Return null;
Var oTempNode = oNode. previussibling;
// Search for the previous sibling nodes one by one until the element nodes are found.
While (oTempNode. nodeType! = 1 & oTempNode. previussibling! = Null)
OTempNode = oTempNode. previussibling;
// Three-object operator. If it is an element node, the node itself is returned; otherwise, null is returned.
Return (oTempNode. nodeType = 1 )? : OTempNode: null;
}

[Access to the next node] compatible with IE and FireFox
(Custom)
Copy codeThe Code is as follows:
Function nextSib (oNode ){
Var oTempLastNode = oNode. parentNode. lastChild;
// Determine whether the node is the last node. If yes, null is returned.
If (oNode = oTempLastNode)
Return null;
Var oTempNode = oNode. nextSibling;
// Search for the following sibling nodes one by one until the element nodes are found.
While (oTempNode. nodeType! = 1 & oTempNode. nextSibling! = Null)
OTempNode = oTempNode. nextSibling;
// Three-object operator. If it is an element node, the node itself is returned; otherwise, null is returned.
Return (oTempNode. nodeType = 1 )? OTempNode: null;
}

(2) determine whether a node has a subnode:
NodeObject. hasChildNodes () method: returns true if childNodes contains one or more nodes.
(3) set node attributes:
EleNode. getAttribute (attrNode) method: returns the attrNode attribute of the eleNode element.
EleNode. setAttribute (attrNode, sNewValue) method: Set the attrNode attribute value of the eleNode element to sNewValue.
(4) create a node:
Document. createElement (eleNode) method: Create an element node eleNode
Document. createTextNode (textNode) method: Create a text node textNode
Document. createDocumentFragment () method: create a document fragment Node
(5) Add a node:
EleNode. appendChild (textNode) method: add the textNode node to the end of childNodes
(6) Delete A node:
ONode. parentNode. removeChild (oNode) method: Delete the oNode node from childNodes
(7) Replacing nodes:
ONode. parentNode. replaceChild (oNewNode, oOldNode) method: replace oOldNode in childNodes with oNewNode
(8) insert a node before a specific node:
OTargetNode. parentNode. insertBefore (oNewNode, oTargetNode) method: insert the oNewNode node before the oTargetNode node in childNodes
(9) insert a node after a specific node:
(Custom) oTargetNode. parentNode. insertAfter (oNewNode, oTargetNode) method: insert the oNewNode node after the oTargetNode node in childNodes
Copy codeThe Code is as follows:
Function insertAfter (oNewNode, oTargetNode ){
Var oParentNode = oTargetNode. parentNode;
If (oParentNode. lastChild = oTargetNode)
OParentNode. appendChild (oNewNode );
Else
OParentNode. insertBefore (oNewNode, oTargetNode. nextSibling );
}

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.