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>