JavaScript DOM Basic Operations summary _javascript Tips

Source: Internet
Author: User

You may also often encounter so summarized as follows:
Creation of 1,dom elements
Insertion of 2,dom elements
3,dom element substitution
Deletion of 4,dom elements

First, there are some simple DOM elements on the page
<div id= "Wrap" >
<div id= "a" >aaaaaaaa</div>
<div id= "B" >bbbbbbbb</div>
<div id= "C" >ccccccccc</div>
</div>
Next we create a DIV element, JS code:
var div_d = document.createelement (' div ');
div_d.innerhtml = "DDDDDDDD";
Div_d.id = "D";
There's no innertext here. To avoid browser compatibility cause some problems;
Then insert the DIV with ID D created in front of the DIV with DOM element ID b
var div_wrap = document.getElementById (' wrap ');
var div_b = document.getElementById (' B ');
Div_wrap.insertbefore (Div_d,div_b);
You can do this if you insert directly behind a child element with ID Wrap:
Div_wrap.insertbefore (Div_d,null);

If it is replaced Div_b is as follows:
Div_wrap.replacechild (Div_d,div_b);
Finally we delete the specified div with an element ID of b
Div_b.parentNode.removeChild (Div_b);
Or
Document.body.removeChild (Div_b);
Follow-up may have a number of derivative methods and applications this is not going to be written anymore.

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.