It can be seen that the insertBefore () method is characterized by inserting a new node before an existing subnode, but the two conditions are combined to find that the insertBefore () method inserts a node, it can be anywhere in the subnode list. First, we can understand the two methods from the definition:
AppendChild () method: you can add a new subnode to the end of the node's subnode list. Syntax: appendChild (newchild)
InsertBefore () method: you can insert a new subnode before an existing subnode. Syntax: insertBefore (newchild, refchild)
Similarities: Insert a subnode
Difference: the Implementation Principles and methods are different.
The appendChild method adds a new node (relative to the parent node) to the end of the child node in the parent node ).
The insertBefore method adds a new node before an existing node (compared to a subnode ).
Let's take a look at this simple example: Add a subnode p at the end of the box-con id
The Code is as follows:
1
2
3
The Code is as follows:
Window. onload = function (){
Var btn = document. getElementById ("creatbtn ");
Btn. onclick = function (){
InsertEle ();
}
}
Function insertEle (){
Var oTest = document. getElementById ("box-one ");
Var newNode = document. createElement ("p ");
NewNode. innerHTML = "This is a newcon ";
// OTest. appendChild (newNode );
OTeset. insertBefore (newNode, null); // both methods can be implemented
}
The first parameter of insertBefore is the same as that of appendChild. It is the new node variable, and the second parameter of insert can be null. You can also write it like this.
The Code is as follows:
Function insertEle (){
Var oTest = document. getElementById ("box-one ");
Var newNode = document. createElement ("p ");
Var reforeNode = document. getElementById ("p1 ");
NewNode. innerHTML = "This is a newcon ";
OTest. insertBefore (newNode, reforeNode); // the newly created element node is inserted before the element whose id is p1.
}
Or
The Code is as follows:
Function insertEle (){
Var oTest = document. getElementById ("box-one ");
Var newNode = document. createElement ("p ");
Var reforeNode = document. getElementById ("p1 ");
NewNode. innerHTML = "This is a newcon ";
OTest. insertBefore (newNode, reforeNode. nextSibling); // the newly created element node is inserted before the node element whose id is p1,
That is to say, insert the id behind the element of the P1 node.
}
Here we will talk about nextSibling: Elements followed by an element (in the same tree level ).
ReforeNode. nextSibling: obtains the next node that follows the reforeNode object.
Previussibling-obtain the previous same-level node of a node
It can be seen that the insertBefore () method is characterized by inserting a new node before an existing subnode, but the two conditions are combined to find that the insertBefore () method inserts a node, it can be anywhere in the subnode list.
Hehahaha... Some methods do not only apply to defined features. As long as they conform to the syntax and combine some attributes, there will always be unexpected gains.
As a beginner, I don't know much about it. Maybe my understanding is still quite simple, and some may be wrong. I hope to give me some help and I don't want to ask anything else, I just want to record the details here and draw your suggestions to promote my growth...