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>