JS node operation

Source: Internet
Author: User

# node Operation #

# # Get Node Method # #

var body = document.body; Get directly through elements
var D1 = document.getElementById ("D1"); Get by ID
var PS = document.getelementsbytagname ("P"); Get by element name
var attrs = d1.attributes; Accessing the attributes of an element
Console.log (["Attribute node:", attrs["id"].nodetype]); The node type that accesses the ID element
Console.log (["Access attribute:", d1.id]);
Console.log (["Access attribute:", D1.getattribute ("id")]);
Console.log (["Text node:", Ps[0].firstchild.nodetype]);
Console.log (["Element node:", Body.nodetype]);
Console.log (PS);

Console.log (d1.firstChild.nextSibling.nextSibling.nextSibling.previousSibling);
var nodes = D1.childnodes; Access all child nodes of D1 and get a collection of similar arrays
Console.log (nodes);
Console.log (D1.parentnode);


# # Add Node # #

Addnode.onclick = function () {
var p = document.createelement ("P"); Creating child elements
var text = document.createTextNode ("paragraph"); Create text content for an element
P.appendchild (text); Add text to the created element
Document.body.appendChild (P); To add a child node to the established parent node

}

*d1.childnodes//All Node Collections *

*d1.parentnodes//Parent Node *

# # Insert Node # #

var insertnode = document.getElementById ("Insertnode");
Insertnode.onclick = function () {
var p = document.createelement ("P");
var text = document.createTextNode ("paragraph");
P.appendchild (text);

var D1 = document.getElementById ("D1");
Document.body.insertBefore (P,D1);

Document. The parent node of the insertion node. insertbefore (new insertion stage, inserted node);

}

# # Delete Node # #

var delnode = document.getElementById ("Delnode");
Delnode.onclick = function () {
Document.body.removeChild (Document.body.lastChild);

Document. Removes the parent node of the node. RemoveChild (remove node);;

}

# # Replacement Node # #

var replacenode = document.getElementById ("ReplaceNode");
Replacenode.onclick = function () {
var H1 = document.createelement ("H1");
var text = document.createTextNode ("title");
H1.appendchild (text);
Document.body.replaceChild (H1,document.body.lastchild);

Document. Replace the parent node of the node. ReplaceChild (node replaced, node replaced);

}

* ' d1.innerhtml + = ' <p>innerhtml add </p> '; ' The original content will be replaced by new content, but inefficient *

There are two elements in the *innerhtml that cannot be replaced: TD in 1.table and Tr;2.select in option*

*innertext the contents of "" As is , and can be used in Web pages where the source code is needed to ensure that the label is not parsed *

JS node operation

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.