DOM (3) JS Create, add element

Source: Internet
Author: User
Tags pear pears

1.createElement (), createTextNode (), AppendChild ()

<Script>                                 varBody=document.getElementsByTagName ('Body')[0]; //Get BODY element                                varDiv=Document.createelement ("Div"); //create element node Direct write element nameBody.appendchild (div);//appendchild () is the addition of child elements, (multiple) By default to the end of child elements                                varStr=document.createTextNode ('Hello');//Create a text nodediv.appendchild (str); </Script>

2.insertBefore (x, y), adding a new x child element before the specified child element y (while appendchild () is the handle element plus the last)

<    ulID= "box">            <Li>Apple</Li>            <LiID= "Li">Pears</Li>        </ul>        <Script>                            varBox=document.getElementById ('Box'); varXG=Document.createelement ('Li'); varStr=document.createTextNode ('Watermelon') Xg.appendchild (str); varLizi=document.getElementById ('Li');        Lizi.parentNode.insertBefore (Xg,lizi); </Script>

3.replaceChild (x, y); Replace the child node Y with the child node X, y including all of its child nodes,

        <ulID= "box">            <Li>Apple</Li>            <LiID= "Li">Pears</Li>        </ul>        <Script>                            varBox=document.getElementById ('Box'); varP=Document.createelement ('P');  Box.parentNode.replaceChild (P,box); Entire UL (including LI) HTML structure, replaced by <p></p></Script>                

The 4.cloneNode () method can be copied from the node.

<     ulID= "box">            <Li>Apple</Li>            <LiID= "Li">Pears</Li>        </ul>        <Script>                varBody=document.getElementsByTagName ('Body')[0]; varBox=document.getElementById ('Box'); varLi=document.getElementById ('Li'); varClone=Li.clonenode (true);  Box.appendchild (clone); //"pear" copy of one add to UL varClone2=Box.clonenode (true); Body.appendchild (Clone2); //Whole UL copy, add to Body </Script>

5. RemoveChild () Removes the specified child nodes

         <ulID= "box">            <Li>Apple</Li>            <LiID= "Li">Pears</Li>        </ul>        <Script>                varBox=document.getElementById ('Box'); Box.removechild (LI); //Remove the pear that one Li </Script>            

DOM (3) JS Create, add element

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.