Javascript DOM node access and operations, jsdom Node
1. Access the DOM Node
1.1 getElememtByld ("tag id ")
1.2 retrieve element set by Tag Name: getElementsByTagName () returns an array of Objects
2. Text in the Operation Element
2.1 The simple method is innerText (), but FireFox is not compatible.
Function text (e ){
Var t = "";
// If the input is an element, continue to traverse its element
// Otherwise, assume it is an array.
E = e. childNodes | e;
// Traverse all the byte points
For (var j = o; j <e. length; j ++ ){
// If it is not an element, append its text value
// Otherwise, recursively traverse the subnodes of all elements
T + = e [j]. nodeType! = 1? E [j]. nodeValue: text (e [j]. childNodes );
}
Return t;
}
Function displayInfo (){
Var x = document. getElementById ("test"); // obtain the element whose id is test.
Alert (text (x); // display its information
}
This is a simple and easy-to-use method to obtain the text content of an element.
3. html in the operation Element
3.1 innerHTML
4. Modify the DOM Node
4.1 create a DOM Node
Document. createElementt ("div ");
4.2 Insert a DOM Node
Common functions for inserting DOM nodes: insertBefore and appendChild.
1. parentOfBeforeNode. insertBefore (nodeToInsert, beforeNode); nodeToInsert is to be inserted
Node. beforeNode indicates the node to which the data is inserted, and parentOfBeforeNode indicates the parent element of beforeNode.
2. appendChild Function
It calls an element parameter to append the specified node to the end of the subnode list.
ParentElement. appendChild (nodeToInsert );
4.3 Delete a dom Node
RemoveChild () function; NodeParent. removeChild (NodeToRemove );