Dom operations-How to add, remove, move, copy, create, and find nodes

Source: Internet
Author: User

document.write只能重绘整个页面innerHTML可以重绘页面的一部分

1) Create a new node
Createdocumentfragment ()//Create a DOM fragment
CreateElement ()//create a specific element
createTextNode ()//Create a text node
(2) Add, remove, replace, insert
AppendChild ()

var node = dcument.createelement ("")
var textnode=document.createtextnode ("Water");
Node.appendchild (Textnode);

RemoveChild ()

var List=document.getelementbyid ("MyList");
List.removechild (List.childnodes[0]);

ReplaceChild ()

document.getElementById ("MyList"). ReplaceChild (Newnode,oldnode);


InsertBefore ()//Insert a new child node before the existing child node

var Node=document.getelementbyid ("MyList2"). LastChild;
var List=document.getelementbyid ("MyList1");
List.insertbefore (Node,list.childnodes[0]);
(3) Find
getElementsByTagName ()//by tag name
Getelementsbyname ()//through the value of the element's Name property (ie is more tolerant of fault tolerance, it will get an array that includes the ID equals the name value)
getElementById ()//through element ID, uniqueness

 



Dom operations-How to add, remove, move, copy, create, and find nodes

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.