Javascript learning notes (18) Get elements on the page

Source: Internet
Author: User
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


    2

  • 3

  • 4

  • 5

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

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.