JavaScript DOM method and attribute Summary

Source: Internet
Author: User

1 createElement (element)
Create a specified tag name and create a new element node. The returned value is the reference pointer pointing to the new element node.
Eg) var para = document. createElement ("p ");
Document. body. appendChild (para );

2 createTextNode ()
Create a new text node that contains the given text and return a reference pointer pointing to the new text node:
Reference = document. createTextNode ()
The parameter is the text string contained in the new text node.
Eg)
Var message = document. createTextNode ("hello world ");
Var container = document. createElement ("p ");
Container. appendChild (message );
Document. body. appendChild (container );

3 cloneNode ()
Reference = node. cloneNode (deep)
Create a copy for a given node. The parameter is true or false. true indicates that the child node of the node is copied at the same time. If false, no child node is copied.
Var para = document. createElement ("p ");
Var message = document. createTextNode ("hello world ");
Para. appendChild (message );
Document. body. appendChild (para );
Var newpara = para. cloneNode (true );
Webjx. Com


Document. body. appendChild (newpara );

4 appendChild ()
Reference = node. appendChild (newChild );
Insert a node. For example, refer to the previous section.

5 insertBefore ()
Reference = element. insertBefore (newNode, targetNode)
Insert a given node to the front of a given element node to the child node, and return a reference pointer pointing to the new child node.
Eg)
Var container = document. getElementById ("content ");
Var message = document. getElementById ("fineprint ");
Var para = document. createElement ("p ");
Container. insertBefore (para, message );

6 removeChild ()
Reference = element. removeChild (node)
Deletes a subnode from a given element and returns a reference pointer pointing to the deleted subnode.
When a node is deleted by removeChild (), all its subnodes are deleted.

7 replaceChild ()
Reference = element. replaceChild (newChild, oldChild)
Replace a child node in a given parent element with another node. The oldChild node must be a child node of the element. The returned value is a pointer to the replaced one.
The reference pointer of the subnode.
Webpage tutorial Network

Eg)
Var container = document. getElementById ("content ");
Var message = document. getElementById ("fineprint ");
Var para = document. createElement ("p ");
Container. replaceChild (para, message );

8 setAttribute ()
Element. setAttribute (attributeName, attributeValue );
Add a new attribute value for a given element node or change its existing attribute.

9 getAttribute
AttributeValue = element. getAttribute (attributeName)
Returns the value of a given attribute node for a given element.

10 getElementById ()
Element = document. getElementById (ID)
Searches for an element with a given id attribute value and returns an element node.

11 getElementByTagName ()
Used to find all elements with a signature for the calibration:
Elements = document. getElementsByTagName (tagName)
Returns a node set.

12 hasChildNodes
Used to check whether a given element has subnodes.
BooleanValue = element. hasChildNodes
Returns true or false.

13 DOM attributes
Node attributes
The nodeName attribute returns a string whose content is the name of the given node.
Webjx. Com


If the node is an element node, the name of the element is returned;
If it is an attribute node, the name of this attribute is returned;
If it is a text node, a string whose content is # text is returned;

The nodeType attribute returns an integer that represents the type of the given node.
The nodeValue attribute returns the current value of the given node.
If the node is an element node, null is returned;
If it is an attribute node, the name of this attribute is returned;
If it is a text node, the content of the text node is returned;

Traverse node tree
ChildNodes this attribute returns an array consisting of subnodes of the given Element Node
FirstChild returns the first subnode
LastChild returns the last subnode
NextSibling returns the next subnode of a given node.
ParentNode returns the parent node of a given node.
Previussibling returns the next subnode of the given Node ??

14 setTimeout
A javaScript function allows a function to be executed after a certain period of time. The function has two parameters. The first parameter is the name of the function to be executed;
The second parameter is a value. In milliseconds, it sets how long it takes to start executing the function provided by the first parameter:
SetTimeout ("function", interval );

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.