Add a new HTML content
We'll learn the four jQuery methods for adding new content:
- Append ()-inserts content at the end of the selected element
- Prepend ()-Inserts content at the beginning of the selected element
- After ()-Inserts the content after the selected element
- Before ()-insert content before selected element
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <MetaCharSet= "Utf-8">5 <Scriptsrc= "Https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js">6 </Script>7 <Script>8 $ (document). Ready (function(){9 varI= 0;Ten $("#btn1"). Click (function(){ One I++ ; A $("P"). Append ("<b>" +I+ "</b>" + " "); - }); - the $("#btn2"). Click (function(){ - $("ol"). Append ("<li> Add a list </li>"); - }); - }); + </Script> - </Head> + A <Body> at <P>Number:</P> - <ol> - <Li>List Item 1</Li> - <Li>List Item 2</Li> - <Li>List Item 3</Li> - </ol> in <ButtonID= "BTN1">Add text numbers</Button> - <ButtonID= "BTN2">Add List item</Button> to <HR> + <Buttononclick= "AppendText ()">Append text</Button> - </Body> the * <Script> $ functionAppendText () {Panax Notoginseng vartxt1="<b> text------</b>"; //Create text using HTML tags - vartxt2=$("<u></u>"). Text ("Text-----"); //Create text using JQuery the varTxt3=Document.createelement ("I"); + txt3.innerhtml="text. "; //using the DOM to create text with Dom A $("Body"). Append (TXT1,TXT2,TXT3); //Append new Element the } + </Script> - </HTML>
jquery Add and remove elements