The InsertBefore, InsertAfter, and appendchild of JavaScript are briefly introduced

Source: Internet
Author: User

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

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.