The HTML dom (Document Object Model)

Source: Internet
Author: User

With the HTML Dom, JavaScript can access all the elements of an HTML document.

The HTML dom (Document Object Model)

When a Web page is loaded, the browser createsDOcumentOBjectMOdel of the page.

TheHTML domModel is constructed as a treeObjects

The html dom tree

With a programmable object model, JavaScript gets all the power it needs to create dynamic HTML:

    • Javascript can change all the HTML elements in the page
    • Javascript can change all the HTML attributes in the page
    • Javascript can change all the CSS styles in the page
    • Javascript can react to all the events in the page
Finding HTML elements

Often, with JavaScript, you want to manipulate HTML elements.

To do so, you have to find the elements first. There are a couple of ways to do this:

    • Finding HTML elements by ID
    • Finding HTML elements by Tag Name
    • Finding HTML elements by Class Name
Finding HTML elements by ID

The easiest way to find HTML elements in the Dom, is by using the element ID.

This example finds the element with ID = "Intro ":

Example
 
VaRX = Document. getelementbyid ("Intro ");

If the element is found, the method will return the element as an object (in X ).

If the element is not found, X will contain null.

Finding HTML elements by Tag Name

This example finds the element with ID = "Main", and then finds all <p> elements inside "Main ":

Example
 
VaRX = Document. getelementbyid ("Main ");VaRY = x. getelementsbytagname ("p ");

 Notice:Finding elements by class name does not work in Internet Explorer 5, 6, 7, and 8.

   

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.