Document Object Model (DOM) series one: Dom Basics

Source: Internet
Author: User

Objective:

With JavaScript, you can refactor an entire HTML document to add, remove, change, or rearrange items on a page. To change a section on a page, JavaScript requires an interface to access all the elements in the HTML document. Through this interface, you can provide methods and properties for adding, moving, changing, or removing HTML elements, all of which are implemented by the Document Object Model (DOM). Simply put, the HTML document represents the page, and the DOM represents how to manipulate the page.

1. Structure of DOM tree

There are three different types of nodes in the DOM node tree, such as element nodes, text nodes, and attribute nodes.

2. Document Object

The Document object represents an HTML file that is displayed in a browser window or frame. When a browser loads an HTML document, it creates a Document object for each HTML document, which is a property of the Window object. The Document object has a number of properties and methods.

The most common functions are as follows:

document.write (): Dynamically writes content to a page.

Document.createelement (TAG): Creates an HTML tag object.

document.getElementById (ID): The object that obtains the specified ID value.

Document.getelementbyname (name): Gets the object that specifies the Name value.

Example A:

var maincontent = document.getElementById ("main");    alert (maincontent);    MainContent.style.backgroundColor = "#FF0000";    var paragraphs = document.getelementsbytagname ("P");        for (var i = 0; i < paragraphs.length;i++)    {        paragraphs[i].style.fontsize = "2em";    }    var elements = document.getElementsByTagName ("Body") [0].childnodes;        for (var i = 0; i < elements.length;i++)    {                  alert (elements[i].nodetype);           }

HTML Document:

<%@ page language= "C #" autoeventwireup= "true" codebehind= "Default.aspx.cs" inherits= "Ui._default"%><! DOCTYPE html>

  

 

Document Object Model (DOM) series one: Dom Basics

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.