Target.insertbefore (Newchild,existingchild)
Parameter description:
1.target: The node being added and the parent node of the existing node.
2.newChild: The node that will be inserted.
3.existingChild: The existing node, the new node will be inserted in front of it, this parameter value can be null.
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.softwhy.com/" /><title>InsertBefore () function-Ant tribe</title> <Scripttype= "Text/javascript">window.onload=function(){ varObox=document.getElementById ("Box"); varLis=document.getElementsByTagName ("Li"); varNewli=Document.createelement ("Li"); Newli.innerhtml="New Ant Tribe"; Obox.insertbefore (newli,lis[1]);}</Script> </Head> <Body> <ulID= "box"> <Li>Ant Tribe One</Li> <Li>Ant Tribe II</Li> <Li>Ant Tribe Three</Li> <Li>Ant Tribe Four</Li></ul></Body> </HTML>
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.softwhy.com/" /><title>InsertBefore () function-Ant tribe</title> <Scripttype= "Text/javascript">window.onload=function(){ varObox=document.getElementById ("Box"); varLis=document.getElementsByTagName ("Li"); varNewli=Document.createelement ("Li"); Newli.innerhtml="New Ant Tribe"; Obox.insertbefore (Newli,NULL);}</Script> </Head> <Body> <ulID= "box"> <Li>Ant Tribe One</Li> <Li>Ant Tribe II</Li> <Li>Ant Tribe Three</Li> <Li>Ant Tribe Four</Li></ul></Body> </HTML>
Two. AppendChild () function:
This function appends a new node to the inner end of the parent node.
Target.appendchild (NewChild)
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.softwhy.com/" /><title>InsertBefore () function-Ant tribe</title> <Scripttype= "Text/javascript">window.onload=function(){ varObox=document.getElementById ("Box"); varLis=document.getElementsByTagName ("Li"); varNewli=Document.createelement ("Li"); Newli.innerhtml="New Ant Tribe"; Obox.appendchild (Newli);}</Script> </Head> <Body> <ulID= "box"> <Li>Ant Tribe One</Li> <Li>Ant Tribe II</Li> <Li>Ant Tribe Three</Li> <Li>Ant Tribe Four</Li></ul></Body> </HTML>
Three. InsertAfter Function:
Although the InsertAfter method is not available in the DOM, we can accomplish this by InsertBefore method: function Insert? After (newelement, targetelement) { var parent = targetelement?. parentnode; if (Parent.lastchild = = targetelement) { ? Parent.appendchild (newelement); }? else { Parent.insertbefore (newelement, targetelement.nextsibling);? }?}?
Now, let's see how this function is done step-to-step.
(1) First, this function has two parameters: one is the new element that will be inserted, and the other is the target element. These two parameters are passed to this function through variables newelement and targetelement;
(2) Save the ParentNode attribute value of the target element to the variable parent;
(3) Next, check that the target element is not the last child element of the parent;
(4) If so, the new element is appended to the parent element using the AppendChild method, so that the new element is inserted exactly after the target element;
(5) If not, the new element is inserted between the target element and the next sibling element of the target element, the next sibling element of the target element is the NextSibling attribute of the target element, and the new element is inserted before the next sibling element of the target element with the InsertBefore method.
The InsertBefore, InsertAfter, and appendchild of JavaScript are briefly introduced