Dom manipulation of JavaScript (node operations)

Source: Internet
Author: User
Tags tag name tagname unique id

Create node
createelement ()
var node = document.createelement ("div");
Nothing to say, create an element node, but note that the node is not automatically added to the document.

2, create a text node
createTextNode ()
var value = document.createTextNode ("text");
Create a text node, add content to the common elements node, and not automatically add to the document.
Many people know innerHTML, do not know this method, this add is static text, if the content inserted without HTML format, with createTextNode than innerHTML security, and innertext have browser incompatibility problem, So it works well with createTextNode.

3, insert node to last
AppendChild ()
Node.appendchild (value);
Inserting the node into the end, the above two created nodes are not automatically added to the document, so the appendchild is inserted.
If the new node is inserted into the last, and if it is already existing node is moved to the end, this is a lot of people do not notice, understand this, and then the following method, you can easily move the operation node.

4, insert the node to the front of the target node
insertbefore ()
var node = document.createelement ("div");
var _p = document.createelement ("P");
var _span = document.createelement ("span");
Node.appendchild (_p);
Node.insertbefore (_span, _p); The
<span> node is inserted in front of the <p> node, where the second parameter is optional, and if the second parameter is not written, it is added by default to the end of the document, which is equivalent to AppendChild.
Similarly, appendchild and insertbefore, if they exist, they will automatically delete the original node and move to the place you specified.
Tips for moving nodes to the front:
if (node.parentNode.firstChild)

Node.parentNode.insertBefore (node, node.parentNode.firstChild);
else Node.parentNode.appendChild (node);

5. Replication nodes
CloneNode (Boolean)
Node.clonenode (TRUE);
Node.clonenode (FALSE);
Copy the above div node, parameter True, copy the entire node and the contents of the inside, false, only copy the contents of the node, the new node after the copy, and will not be automatically inserted into the document, need to use 3 and 4 method to insert.

6. Deleting nodes
RemoveChild ()
Node.removechild (_p);
Remove the above <p> nodes from the <div>. In general, however, you do not know what the parent node of the node you want to delete is, so this is generally the case: node.parentNode.removeChild (node);

7. Replace node
Repalcechild (NewNode, OldNode)
Node.repalcechild (_p, _span);
Replace the <span> node above with the <p> node, note that either <span> or &LT;P&GT, it must be a child node of <div> or a new node.

8. Set node properties
SetAttribute ()
Node.setattribute ("title", "abc");
Not explained, it's easy to understand. Just to say, using this method to set the node properties is good, but the class attribute cannot be set.

9. Get Node Properties
GetAttribute ()
Node.getattribute ("title");
Same as 8, gets the node properties.

10. Determine if the element has child nodes
HasChildNodes
Node.haschildnodes;
Returns a Boolean type, so insert the new node to the front tip:
var node = document.createelement ("div");
var newNode = document.createelement ("P");
if (node.haschildnodes) Node.insertbefore (NewNode, node.firstchild);
else Node.appendchild (node);


Finally, the properties of the DOM are:

NodeName-the name of the node;
NodeType-Returns an integer representing the type of the node, 1-element node, 2-Attribute node, 3-text node;
NodeValue-Returns a string that is the value of the node;
ChildNodes-Returns an array consisting of the child nodes of the element node;
FirstChild-Returns the first child node;
LastChild-Returns the last child node;
NextSibling-Returns the next sibling node of the target node, or null if the target node does not have a node that belongs to a parent node;
PreviousSibling-Returns the previous sibling node of the target node, or null if there are no nodes in front of the target node that belong to a parent node;
ParentNode-The returned node is always an element node, because only the element node is likely to have child nodes, and the document node returns null;

JavaScript Operations Dom establishes an additional delete clone Access node Example:

1. getElementById (ID)

This is one of the most common examples of accessing an element through an ID:

Note: If the ID of the element is not unique, then the first element of that ID name will be taken

2. Getelementsbyname (name)

This is through the name to get a bunch of elements (as an array), see the element behind a small s to know that the ID is the HTML document requires unique, name can not be unique, such as checkbox, radio and other places will use more than Input uses the same name to identify whether it is an accomplice. Yes, Getelementsbyname (name) is only used to get input, radio, checkbox and other elements, such as <inputname= "MyRadio" type= "Radio"/>

3. getElementsByTagName (tagname) Look at this method to know that this is also to get a bunch of elements (as an array), is obtained by tagname is the tag name. You can iterate through this array to get each individual element. When a DOM structure is large, it can be used to effectively narrow the scope of the search.

  


4. AppendChild (node)

Appends a node to the current element, which should be called the object more appropriately.

Just now I was in the first example in order to show the content, using the innerHTML, just saw the article just learned that innerHTML does not belong to the DOM.

5. RemoveChild (childreference)

Deletes the child node of the current node and returns the node that was deleted.
This deleted node can be inserted somewhere else.

  

6. CloneNode (Deepboolean)

Copies and returns the replication node of the current node, which is an orphaned node that replicates the properties of the original node and, if necessary, modifies the ID attribute to ensure the unique ID of the new node before it is added to the document.

This method supports a Boolean parameter that, when Deepboolean is set to true, copies all the child nodes of the current node, including the text within that node.

  

7. ReplaceChild (NewChild, Oldchild)

Change one child node of the current node to another node

  

JavaScript Operations Dom establishes an additional delete clone Access node Example:

1. getElementById (ID) This is one of the most common examples of access to an element by ID:

Note: If the ID of the element is not unique, then the first element of that ID name will be taken

2. Getelementsbyname (name) This is the use of name to get a bunch of elements (as an array), see the element behind a small s to know, ID is required in the HTML document, name can not be unique, such as CheckBox, Radio and other places will use multiple input with the same name to identify whether it is an accomplice. Yes, Getelementsbyname (name) is only used to get input, radio, checkbox and other elements, such as <input name= "MyRadio" type= "Radio"/>

3. getElementsByTagName (tagname) Look at this method to know that this is also to get a bunch of elements (as an array), is obtained by tagname is the tag name. You can iterate through this array to get each individual element. When a DOM structure is large, it can be used to effectively narrow the scope of the search.

  


4. AppendChild (node)
Appends a node to the current element, which should be called the object more appropriately.

Just now I was in the first example in order to show the content, using the innerHTML, just saw the article just learned that innerHTML does not belong to the DOM.

5. RemoveChild (childreference)

Deletes the child node of the current node and returns the node that was deleted.
This deleted node can be inserted somewhere else.

  

6. CloneNode (Deepboolean)

Copies and returns the replication node of the current node, which is an orphaned node that replicates the properties of the original node and, if necessary, modifies the ID attribute to ensure the unique ID of the new node before it is added to the document.

This method supports a Boolean parameter that, when Deepboolean is set to true, copies all the child nodes of the current node, including the text within that node.

  

7. ReplaceChild (NewChild, Oldchild)

Change one child node of the current node to another node

  

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.