The usage and application of JS DOM

Source: Internet
Author: User

Dom Introduction: Document Object Model

It is convenient for JavaScript to manipulate HTML through DOM.

Node classification in HTML:

1. Documentation Node (document)

2. ELEMENT nodes

3. Text node

4. Attribute nodes

5. Note Node

Selection of nodes

1.document.getelementbyid (id attribute value);

2.document.getelementsbytagname (tag tag name);

3.document.getelementsbyname (Name property value); not recommended

Note: 1. The collected elements are returned as strings.

Method of obtaining a text node (Nodes: node)

var dd = document.getelementsbytagname (' div ') [0];

FirstChild, LastChild: Parent node gets first/last child node

NextSibling: Get the next sibling node

PreviousSibling: Get the last sibling node

ChildNodes: Parent node obtains all internal child node information

Get property value

1. Get Property values

Itnode. Attribute name;

Itnode.getattribute (attribute name);

2. Setting Property values

Itnode. Property name = value;

Itnode.setattribute (name, value);

Obtaining a Property node

var attrlist = itnode.attributes;

Attrlist. Attribute name;

Creation of nodes

Element node: document.createelement (tag tag name);

Text node: document.createTextNode (text content);

Property settings: Node.setattribute (name, value);

Increase of nodes

Parent node. appendchild (child node);

Parent node. insertbefore (Newnode,oldnode); NewNode put it in front of the OldNode.

Parent node. replacechild (Newnode,oldnode); NewNode Replace to OldNode node

Get CSS Styles

Element node. style.css style name;; Get width style

Set CSS styles (modified, not added)

Element node. STYLE.CSS style name = value; = ' 500px '; set div width style

The usage and application of JS DOM

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: 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.