Getting Started with JavaScript (3)

Source: Internet
Author: User

I. Knowledge of the DOM

The Document Object Model DOM defines a standard way to access and manipulate HTML documents. The DOM renders an HTML document as a tree structure with elements, attributes, and text (the node tree).

Eg:

To decompose the HTML code into a DOM node hierarchy diagram:

An HTML document can say a collection of nodes, three common DOM nodes:

1, Element node:

2. Text node: Content that is presented to the user, such as JavaScript, DOM, CSS, and other text in <li>...</li>.

3. Attribute nodes: element attributes, such as link properties for <a> tags href= "http://www.imooc.com"

EG2:

<a href= "http://www.imooc.com" >javascript dom</a>

Second, get the element by ID

The id attribute value of the tag is unique, just like " everyone has a social security number, can find the corresponding person through the ID number ", in the Web page first find the label by ID, and then operate

Grammar:

document.getElementById ("id")

Eg:

Result: null or [Object Htmlparagraphelement]

Note: Gets the element is an object, if the element is manipulated, we want to pass its property or method.

Third, innerHTML properties

The innerHTML property is used to get or replace the contents of an HTML element.

Grammar:

Object.innerhtml

Note:

1. Object is the object that gets the element, such as by document.getElementById ("ID");

2, pay attention to write, innerHTML case-sensitive.

Eg: Get <p> elements by id= "con" and output and change elements content

Iv. Changing HTML styles

Grammar:

Object.style.property=new style;

Note: object is an element that gets the element object, such as by document.getElementById ("id").

Partial basic attribute table (property)

Eg: Change the <p> element style, convert the color to red, change the font size to 20, and change the background color to blue.

{

<p id= "Pcon" >hello world!</p>

<script>

var MyChar = document.getElementById ("Pcon");

Mychar.style.color= "Red";

Mychar.style.fontsize= "20";

Mychar.style.backgroundcolor= "Blue";

</script>

}

V. Show and hide (Display properties)

Grammar:

Object.style.display = value

Note: object is an element that gets the element object, such as through document.getElementById ("id")

Value:

Vi. Control class name (ClassName attribute)

ClassName property Sets or returns the Class property of an element

Grammar:

Object.classname = ClassName

Role:

1. Get the class attribute of the element

2. Specify a CSS style for an element within the page to change the appearance of the element

Eg: Get the class attribute of the <p> element and change the classname:

EG2:

Getting Started with JavaScript (3)

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.