HTML Dom access nodes translation w3schools.com

Source: Internet
Author: User
HTML dom Access nodes

With the Dom, you can access every node in an HTML document.(With Dom, you can access every node in the HTML document)

Accessing nodes

You can access a node in three ways:(You can use three methods to access the node)

    1. By using the getelementbyid () method(Use the getelementbyid method)
    2. By using the getelementsbytagname () method(By using the getelementsbytagname method)
    3. By navigating the node tree, using the node relationship(Use the relationship between nodes to navigate the node tree)

Supplement: 1. Besides using getelementsbytagname, you can also use the getelementsbyname method to access the nodes.

2. In addition to the ID attribute, you can also use the classname attribute to access the node.

Example:

<SCRIPT type = "text/JavaScript">
// <! --
Window. onload = function (){

VaR I, A, S = "";

VaR d = Document. getelementsbytagname ("Div ");// Obtain the HTML elements whose Tag Name Is Div

For (I = 0; I <D. length; I ++)// Traverse the HTML elements whose Tag Name Is Div

If (d [I]. classname = "Q2 ")// Obtain the HTML elements whose Tag Name Is Div and whose calss name is Q2

A = d [I]. getelementsbytagname ("");// Obtain the HTML elements whose Tag Name Is Div and whose Tag Name Is A in the HTML element whose calss name is Q2

For (I = 0; I <A. length; I ++)// Traverse the HTML elements whose tags are Div and whose tags are a in the HTML element whose calss is Q2

S + = A [I]. innerhtml;

Alert (s );
}
// -->
</SCRIPT>

--------------------------------------------------------------

There are many ways to solve a problem in HTML, CSS, and JS learning, and they have great flexibility. Therefore, it is necessary to have a systematic understanding of these methods, rather than one-sided understanding of a method, it is a good way to classify data.

    • access node

      • absolute access

        • element

          • getelementbyid
        • set
          • getelementbytag
          • getelementbyname
          • using class attributes
      • relative access method
        • parent-child/sibling relationship

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.