JavaScript html dom, javascripthtmldom

Source: Internet
Author: User

JavaScript html dom, javascripthtmldom
With html dom, you can access all elements of JavaScript HTML documents.

Html dom (Document Object Model)

When a webpage is loaded, the browser creates a Document Object Model on the page ).

HTML DOMThe model is constructedObjectTree:

Html dom tree

Through the programmable object model, JavaScript has sufficient capabilities to create dynamic HTML.

  • JavaScript can change all HTML elements on the page
  • JavaScript can change all HTML attributes on the page
  • JavaScript can change all CSS styles on the page
  • JavaScript can respond to all events on the page
Search for HTML elements

Generally, you need to operate HTML elements through JavaScript.

To do this, you must first find this element. There are three ways to do this:

  • Find HTML elements by id
  • Find HTML elements by Tag Name
  • Find HTML elements by Class Name
Search for HTML elements by id

The simplest way to search for HTML elements in the DOM is to use the element id.

In this example, find the id = "intro" element:

Instance

Var x = document. getElementById ("intro ");

If this element is found, the method returns the element in the form of an object (in x.

If this element is not found, x contains null.

Search for HTML elements by Tag Name

In this example, find the element id = "main", and then find all <p> elements in the id = "main" element:

Instance

Var x = document. getElementById ("main ");
Var y = x. getElementsByTagName ("p ");


Find HTML elements by Class Name

In this exampleGetElementsByClassNameFunction to find the elements of class = "intro:

Instance

Var x = document. getElementsByClassName ("intro ");


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.