Javascript DOM node access and operations, jsdom Node

Source: Internet
Author: User

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 );

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.