Javascript DOM Programming

Source: Internet
Author: User

Javascript DOM Programming

Access HTML elements

→ Access HTML elements by ID.

→ Access HTML elements based on node relationships.

 

Commonly used

Document. getElementById ("idVal ");

The node whose ID is idVal is obtained.

Document. getElementsByName ("name ");

The returned tag set with the name attribute as name

Document. getElementsByTagName ("input ")

Returns the set of all input tags.

 

---------------- Addition, deletion, modification, and query of DOM

Add:

 

1. Create a node using document. createElement (Tag). The Tag must be a legal HTML Tag name.

The replication Node uses Node cloneNode (boolean deep). When deep is set to true, it indicates that when the current Node is copied, it also copies all the content in it. Otherwise, it copies itself.

2. Add the Node appendChild (Node newNode) as the last Node.

InsertBefore (Node newNode, Node refNode) is added to the front of the refNode Node

ReplaceChild (Node newNode, Node oldNode) replace Node

Delete:

Delete a node using removeChild (oldNode) to delete the oldNode node parameter. The object can also be a number.

 

Change:

Obtain the element to be modified and then modify innerHTML or its value.

 

Query:

 

Access Node: Use document. getElementById ("id ");

You can also use the sibling relationship between nodes.

Access form: the first element in the form is returned using document. getElementById ('form'). elements [0.

Document. getElementById ('form'). elements ['pass'] returns the form element whose name is pass.

Access the list box: document. getElementById ('select'). options [n] select the nth element in the list.

 

Access table element: document. getElementById ('table'). row [1]. cells [2] returns the third row of table 2nd.

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.