Dom InsertBefore () removechild ()

Source: Internet
Author: User

InsertBefore ():
As the name implies, is to insert a new node in front of the target node.
Element.insertbefore (NewNode, Targernode);

The second argument is optional, and if the second argument is not written, the default is added to the end of the document, equivalent to AppendChild ();
Let's see how the InsertBefore () is used:
<div id= "Cssrian" >
<p id= "Content" >1111</p>
<div id= "MSG" >msg<div>test</div></div>
<p id= "Content" >222</p>
<p id= "AAA" >aaaaaaaa</p>
</div>
<script>
var msg = document.getElementById ("msg");
var AAA = document.getElementById ("AAA");
var test = document.getElementById ("Cssrian");
Test.insertbefore (msg, AAA);
</script>
We found that the ID msg was inserted into the front of AAA.
JS Internal processing mode is similar to AppendChild ().

The


deletes the node.
RemoveChild ()   :
<body>
 <div id= "Cssrain"
 <div id= "a" >a </div>
 <div id= "B" >b </div>
 <div id= "C" >c </div>
 </div
 </body>
<script>
var msg = document.getElementById ("Cssrain");
var B = document.getElementById ("B");
 msg.removechild (b);
</script>
If you do not know what the parent node of the node you want to delete is? You can use the ParentNode property.
For example:
<body>
 <div id= "Cssrain"
 <div id= "a" >a </div>
 < Div id= "B" >b </div>
 <div id= "C" >c </div>
 </div>
 </body
<script>
var b = document.getElementById ("b");
var c = b.parentnode;
C.removechild (b);
;/script>

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.