Code for adding and deleting elements with JQUERY

Source: Internet
Author: User

Add and delete elements with JQUERY
JQuery provides methods to add and delete elements.
It can be divided into internal insertion, external insertion, package replacement, and deletion.
Main Internal insertion methods:
Append (content) adds content to each matching element.
Prepend (content) pre-content to each matching element.
External insert:
After (content) inserts content after each matching element.
Before (content) inserts content before each matching element.
Delete:
Empty () deletes all child nodes in the matched element set.
Remove ([expr]) removes all matching elements from the DOM.

The following example shows how to Insert a new row to the table by clicking the Add button.
Click Delete to delete the last row.

Copy codeThe Code is as follows: <! Doctype html public "-// W3C // dtd html 4.01 // EN" "http://www.w3.org/TR/html4/strict.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = iso-8859-1">
<Title> add or delete table elements </title>
<Script type = "text/javascript" src = "jquery-1.4.2.min.js"> </script>
<Style type = "text/css">
Body {font-size: 12px ;}
</Style>
<Script type = "text/javascript">
/* Add prediction */
Function add_tr (){
$ ("# Table1"). append ("<tr>" + $ ("# table1 tr" ).eq(0).html () + "</tr> ");
}
/* Delete prediction */
Function remove_tr (){
// Alert ($ ("# table1 tr" pai.last(pai.html ());
If ($ ("# table1 tr"). size ()> 1 ){
$ ("# Table1 tr: last-child"). remove ();
}
Else
Alert ("this is the last row and cannot be deleted ");
}
</Script>
</Head>
<Body align = 'center'>
<Center>
<Table id = 'table1'>
<Tr>
<Td width = '000000'> the first grid </td>
<Td width = '000000'> <input type = 'text'/> </td>
</Tr>
</Table>
<Br/>
<Input type = 'button 'value = 'add a line 'onclick = 'add _ tr ()'/>
<Input type = 'button 'value = 'Delete the last row 'onclick = 'remove _ tr ()'/>
</Center>
</Body>
</Html>

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.