DOM model for javascript Learning (1)

Source: Internet
Author: User
UmentElement; \\\\\ method 1 varoheadohtml. firstChild; varobodyohtml. lastChild ;\\\\\\ method 2 varoheadohtml. childNodes [0] varobodyohtml. c...

1. Access related nodes
Var ohtml = document.doc umentElement;
\\\\\ Method 1
Var ohead = ohtml. firstChild;
Var obody = ohtml. lastChild;
Method 2
Var ohead = ohtml. childNodes [0]
Var obody = ohtml. childNodes [1];
GetElementsByTagname ("xxx ")
Obtains the Element Set of tags named xxx.
GetElementsByName ("xxx ")
Obtains a set of elements named xxx.
Element. getAttribute ("") setAttribute ("xx", newvalue) removeAttribute ("xx ")
Add, delete, modify, and query attributes
GetElementById ("ddd ")
Get the element with the id of ddd
2. add, delete, modify, and query nodes
Add
Var op = document. createElement ("p ")
Var otext = document. createTextNode ("Helloworld ")
Op. appendChild (otextf)
Document. body. appendChild (op)
Delete
Var op = document. getElementById ("aa ")
Document. body. removeChild (op)
However, it is best to use the parent node to ensure that the parent node of the target node is accessed each time to avoid confusion as follows:
Op. parentNode. removeChild (op );
Change
Var onew = new node
Var old = document. body. getElementById ("aa ")
Old. parentNode. replaceChild (onew, old)
Insert
Var onew = new node
Var old = document. body. getElementById ("aa ")
Old. parentNode. insertBefore (onew, old)

3. Document fragmentation
When you need to create multiple contents at a time and insert them into the page, because each insert operation will cause a page update, you need
Create a document fragment, insert the new document, and add it to the page at one time.
Var frag = document. createDocumentFragment ();
For (var I = 0; I <10; I ++ ){
Var op = document. createElement ("p ")
Var otext = document. createTextNode ("oh! Shit! ")
Op. appendChild (otext );
Frag. appendChild (op );
}
Document. body. appendChild (frag );


Author: carmazhao

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.