nodes and their types
In JavaScript, nodes are divided into three types:
- ELEMENT node: An HTML tag element.
- Attribute node: An attribute of an element that can be manipulated directly by property.
- Text node: is a child node of an element node whose contents are text.
Where do you write the JS code in the HTML document?
In general, before the body node to write the JS code, but need to take advantage of the Window.onload event, which is triggered after the current document is fully loaded, so that the code can get to any node in the current document
function() { var citynode = document.getElementById ("city"); alert (Citynode); }; </script> ...
the properties of the node (all types of nodes have properties)
- NodeName: Represents the name of the current node, read-only property, if the given node is a text node, the NodeName property returns a string with the #text content
- NodeType: Returns an integer, read-only attribute, 1--element node, 2--attribute node, 3--text node, this value represents the type of a given node
- NodeValue: Returns the current value (string) of a given node, a read-write property, where three elements are returned:
- Element node, return value is null
- Attribute node: The return value is the value of this property
- Text node: The return value is the content of this text node
get element Node
- document.getElementById: Gets the corresponding individual node based on the ID property, and returns null if no node exists for that ID. This method can only be used for document objects
- document.getElementsByTagName: Gets an array of the name of the specified node according to the tag name, the length property of the array object can get the lengths of the array (it does not have to be used throughout the document, it can also be used in the child nodes of a particular element to find the element with a given signature)
var document.getElementById ("content"); var paras = container.getelementsbytagname ("P"); alert (paras.length);
- Document.getelementsbyname: gets an array of nodes that match the criteria based on the Name property of the node
- Other two methods, IE is not supported at all, so it is not recommended to use
gets the child nodes of the element node (only the element nodes have child nodes)
- The ChildNodes property gets all the child nodes, but the method is not practical, because if you want to get the specified node a collection of specified child nodes that can be directly called by the getElementsByTagName () method of the element node. Neither the text node nor the attribute node can contain any child nodes, so their ChildNodes property will always return an empty array
- The FirstChild property gets the first child node, neither the text node nor the attribute node can contain any child nodes, so their FirstChild property will always return null
- LastChild property gets the last child node, ibid.
other common properties and methods
nextSibling: Returns the next child node of a given node
previoussibling: Returns the previous child node of a given node
parentnode: Returns the parent node of a given node. The node returned by the ParentNode property is always an element node, because only the element node can contain child nodes, and there is no parent node for the document node
haschildnodes (): This method is used to check whether an element has child nodes, and the return value is TRUE or False
var booleanvalue = Element.haschildnodes ();
Text nodes and attribute nodes can no longer contain any child nodes, so the return value of using the HasChildNodes method for both types of nodes is always false
If the return value of the HasChildNodes method is false, then Childnodes,firstchild,lastchild will be an empty array and an empty string
Get Attribute node
- You can get and set the value of an attribute node in a way that is directly through the element node. Properties (more commonly used)
- With the GetAttributeNode method of the element node to get the attribute node, and then read and write the property value through NodeValue, the value of the given property is returned as a string, and if the given property does not exist, getattribute () returns an empty string
Get text node
Step: Element node--Gets the child node of the element node, if the element node has only one child node of the text node, for example
<id= "BJ" name= "Beijing"> Beijing </li >
You can get to the specified element node Elenode first, and then use the EleNode.firstChild.nodeValue method to read and write the value of its text node
Create a nodeCreate an element node:
createelement (): Creates a new element node according to the given label name. Method has only one parameter: the name of the element node being created, is a string, and the return value of the method is a reference pointer to the new node. The return value is an element node, so its NodeType property value equals 1
Note : The new element node is not automatically added to the document, it is just an object that exists in the JavaScript context
Create a text node:
createTextNode (): Creates a new text node containing the given text, the return value of this method is a pointer to the new text node reference, which is a text node, so its NodeType property equals 3
Method has only one argument: the text string that the new text node contains
Note : New element nodes are not automatically added to the document
to add a child node to an element node:
appendchild (): for example var reference = Element.appendchild (NewChild); The given child node NewChild becomes the last child node of the given element node, the return value of the method is a reference pointer to the new child node
Delete a node
removechild (): Removes a child node from a given element
var reference = element.removechild (node);
The return value is a reference pointer to a child node that has been deleted, and when a node RemoveChild () method is deleted, all child nodes contained by that node are deleted at the same time. If you want to delete a node but don't know which parent it is, the parentnode (get parent node) property can help
replacement of a node
replacechild (): Replaces one child node in a given parent element with another child node
Var
The return value is a reference pointer to the child node that has been replaced (Oldchild)
Note : This node has a moving function in addition to the replacement function. This method can only complete one-way replacements, and if you need to use bidirectional substitution, you need a custom function:
/** * Swap anode and BNode * @param {object} Anode * @param {object} BNode*/ functionReplaceeach (Anode, BNode) {if(Anode = =BNode) { return; } varAparentnode =Anode.parentnode; //If anode has a parent node if(aparentnode) {varBparentnode =Bnode.parentnode; //if BNode has a parent node if(bparentnode) {varTempnode = Anode.clonenode (true); Bparentnode.replacechild (Tempnode, BNode); Aparentnode.replacechild (BNode, anode); } } }
inserting Nodes
insertbefore (): Inserts a given node in front of a given child node of a given element node
var reference = Element.insertbefore (Newnode,targetnode);
The node NewNode will be inserted into the element node elements and appear before the node TargetNode, and the node TargetNode must be a child of element
Note : There is no method that is inserted behind the given child node of the given element node, and the custom function is required:
/** * Insert newChild behind Refchild * @param {object} NewChild * @param {object} refchild*/ functionInsertAfter (NewChild, refchild) {varRefparentnode =Refchild.parentnode; //determine if the Refchild has a parent node if(refparentnode) {//determines whether the Refchild node is the last child node of its parent node if(Refchild = =refparentnode.lastchild) {Refparentnode.appendchild (newChild); }Else{refparentnode.insertbefore (newChild, refchild.nextsibling); } } }innerHTML Property
This property is supported by almost all browsers, but is not part of the DOM standard. The InnerHTML property can be used to read/write HTML content in a given element
Code Listing 1: Displaying HTML content for a given element
Code Listing 2: Replacing HTML content for a given element
Code Listing 3: Exchanging HTML content for an element
Operation of the DOM in Java EE note--javascript