1. the getElementById () method is used to obtain the element id. An element id is obtained by taking a parameter, if this id does not exist, null is returned. Be sure not to make the name of the form element the same as the id of other elements. ie browsers under IE8 use this method to use the name of the element... syntaxHighlighter.
1. Get Elements
The getElementById () method is used to obtain the element id. If the element id does not exist, null is returned.
Make sure that the name of the form element is not the same as the id of other elements. ie browsers under IE8 can use this method to obtain the element through the name attribute of the element.
The following surface element is used as an example.
Here is the div content with id "myDiv"
Var document. getElementById ("myDiv"); // "myDiv" case sensitive, get element reference
The getElementsByTagName () method obtains an element through the element's tag name. It accepts a parameter, that is, the tag name of the element to be obtained, and returns a NodeList containing 0 or more.
1 var images = document. getElementsByTagName ("img"); // obtain all elements on the page
2
3 alert (images. length); // number of images
4 alert (images [0]. src); // src of the first image Element
5 alert (images. item (0). src); // same as above
The getElementsByName () method obtains an element through the name attribute of the element. It accepts a parameter that is used to obtain the name attribute of the element. It is often used to obtain the single-choice button.
1
Var radios = document. getElementsByName ("color"); // obtain all radio buttons for name = "color"
2. Obtain element subnodes or element subnodes and their child nodes
1
2
- Project 1
3
- Project 2
4
- Project 3
5
IE thinks
The element has three subnodes, three of which are considered by other browsers to have seven subnodes, including three elements and four text nodes.
In a row:
- Project 1
- Project 2
- Project 3
Any browser considers three subnodes
Obtain the child nodes of an element:
1 var ul = document. getElementById ("myList ");
2 for (var I = 0, len = ul. childNodes. length; I <len; I ++ ){
3 if (ul. childNodes. length [I]. nodeType = 1) {// nodeType = 1 indicates that the node is an element node, not a text node.
4 // perform some operations
5}
6}
Obtain the child nodes of an element and its child nodes:
1 var ul = document. getElementById ("myList ");
2 var items = ul. getElementsByTagName ("li"); // li in li will also be obtained
From Sunday walk