DOM methods in javascript

Source: Internet
Author: User
DOM is the model of Html pages. Every tag is used as an object. by calling the attributes and methods in DOM, JavaScript can program and control the text box, layer, and other elements in the webpage. For example, you can read the value in the text box and set the value in the text box by operating the DOM object of the text box. 1. Three Methods for getting objects

1) document. getElementById (id); obtained through the object's property id;

2) element. getElementByTagName (tag); obtained by tag name;

3) element. getElementByClassName (class); obtained by the attribute class name;

2: set and get methods of the attribute.

1) object. getAttribute (attribute); get the attributes of an element. ==>>> HTMLDOM: object. attribute;

2) object. setAttribute (attribute, value); set the attribute of the element. ====>>> HTMLDOM: object. attribute = value;

3: Some node attributes

1) childNodes attribute: returns an array.

Element. childNodes [0] is equivalent to element. firstChild.

Element. childNodes [element. childNodes. length] is equivalent to element. lastChild.

2) nodeType attribute: there are 12 possible values, but only 3 of them are useful.

Element Node nodeType = 1

Attribute node nodeType = 2

Text node nodeType = 3

3) nodeValue attribute: The main function is to change the text content of an element. The function is similar to object. innerHTML.

For example:

This is a text node

Var p = document. getElementById ("p ");

P. childNodes [0]. nodeValue = "change the content of a text node ";

In this way, the content in the p node is changed;

You can also achieve this: p. innerHTML = "change the content of a text node ";

The above is all the content of this article. I hope you will like it.

For more information about DOM methods in javascript, please follow the PHP Chinese website!

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.