jquery Add, remove elements

Source: Internet
Author: User

First, JQuery-adding elements

1.append ()-Inserts the specified content at the end of the selected element

2.prepend ()-Inserts the specified content at the beginning of the selected element

3.after ()-Insert content after selected element

4.before ()-Insert content before selected element

Add a single element

<script type= "Text/javascript" src= "Jquery-1.11.2.min.js" ></script><body><p>111</p ></body>$ (document). Ready (function (e) {$ ("P"). Append ("222"); $ ("P"). Prepend ("333");  $ ("P"). After ("444");   $ ("P"). Before ("555");}); </script>

Add several elements---- Focus

<body><button onclick= "Tianjia ()" > Append text </button></body>functionTianjia () {vartxt1= "<p> text 1</p>";//Create text using HTML tags    vartxt2=$ ("<p></p>"). Text ("Text 2");//Create text using JQuery    varTxt3=document.createelement ("P"); Txt3. innerhtml= "Text 3";//using the DOM to create text with Dom$ ("Body"). Append (TXT1,TXT2,TXT3);//Append new Element}</script>

Ii. JQuery-Delete Element

1.remove ()-delete the selected element (and its child elements)

2.empty ()- removes child elements from the selected element

<div id= "Div1" >111111<p>2222222</p><pclass= "PP" >3333333333</p><pclass= "pp" >444444444</p></div><br><button> remove div element </button></body>$ (document). Ready (function(){  $("button"). Click (function(){    $("#div1"). Remove ();//Delete div and all the contents of P inside it         $("#div1").Empty();//Delete the contents of the P tag inside the Div           $("P"). Remove (". pp");//Remove all P labels that are class pp  });});</script>

jquery Add, remove elements

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.