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