Dynamic DOM node js implementation

Source: Internet
Author: User

Recently, I read the book "javascript Object-Oriented Programming Guide" again, read the browser environment chapter, and gradually understand that js, as a script language, must rely on the host environment to implement functions. From the browser perspective, is to operate the BOM and DOM of the browser. DOM operations can be divided into access, modification, deletion, and creation. Each operation has unique methods and attributes. The following example describes how to access, create, and delete a dom node (refer to the article ).

  
 
 
Function add () // example of dynamically adding a table {var parNode = document. getElementById ("Table1"); var tr = document. createElement ("tr"); var td = document. createElement ("td"); // create a td-type Element node td. appendChild (document. createTextNode ("This is the first column"); tr. appendChild (td); var td = document. createElement ("td"); // create a td-type Element node td. appendChild (document. createTextNode ("this is the second column"); tr. appendChild (td); parNode. appendChild (tr);} function remove () {var parNode = document. getElementById ("Table1"); var tr = document. getElementsByTagName ("tr"); alert (tr); // from this point, we can see that tr is a node table. Therefore, to delete a node, We need to specify which node needs to be deleted. removeChild (tr [0]);}

First, create two button elements in html and click the add and remove functions. GetElementById and getElementByTagName in the two functions are quick methods for dom element access. createElement and CreateTextNode are the methods for creating elements and text content respectively, removeChild also answers the comments in the reference article to remove sub-elements.


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.