JavaScript DOM Programming

Source: Internet
Author: User

1. Get the root node of the DOM tree

<script type= The DocumentElement property of the "Text/javascript" >  //document object: Directly to the root tag in the HTML page. Console.info ( document.documentelement);  </script>


2.getElementById: Gets the tag element and getElementsByTagName according to the tag's ID, gets the label based on the tag name, returns the form of the array

<script type= "Text/javascript" >//gets the id= "username" tab in the HTML page. var username = document.getElementById ("username") ; alert (Username.value), var inputs = document.getElementsByTagName ("input") [0];alert (inputs.value)  </script >
3.getElementsByNames: Gets the label based on the label's Name property, returning the array form

<body><input type= "text" id= "username1" name= "username" value= "username1" ><input type= "text" id= " UserName2 "name=" username "value=" username2 ">  </body>  <script type=" Text/javascript ">  Gets the Name= "username" label in the HTML page. var usernames = Document.getelementsbyname ("username"); alert (usernames.length);// Print out the Value property values for each Name= "username" label individually  </script>
There are two input tags, so the print result is 2

4. Create a Label

<body>    <ul id= "City" >    <li id= "BJ" > Beijing </li><li id= "TJ" > Tianjin </li><li id= "NJ" > Nanjing </li>    </ul>  </body>  <script type= "Text/javascript" >  //Create < Li id= "sh" > Shanghai </li> Tag, add this label to the id= "City" tab.//1 create <li id= "sh" > Shanghai </li>//a Create <li></li> Element node var lielement = document.createelement ("li");//b create "Shanghai" text node var text = document.createTextNode ("Shanghai");//c The text node is added by adding the child nodes through the element nodes. Lielement.appendchild (text);//li tags are called element nodes or elements. If you parse it as an element, property is the attribute of the element. Lielement.setattribute ("id", "sh");//2 get id= "City" label var city = document.getElementById ("city");//3 Add City.appendchild (lielement);  </script>




Description of the tree structure: two ways to parse the DOM tree: element Mode and node mode

the difference between an element and a node :

using element Interpretation HTML page :

The tag is the element , the text is the text of the element , and the attribute is the attribute of the element .

using nodes to interpret html page :

tags are element nodes , text is text nodes , attributes are attribute nodes .


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JavaScript DOM Programming

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.