Use javascript to delete the current row and add the row (sample code)

Source: Internet
Author: User

Delete row
Copy codeThe Code is as follows:
<Script>
Function del (obj)
{
Obj. parentNode. parentNode. removeNode (true );
}
</Script>
<Body>
<Table>
<Tr>
<Td> cell 1 </td>
<Td> <input type = button value = "delete this row" onclick = "del (this)"> </td>
</Tr>
<Tr>
<Td> Cell 2 </td>
<Td> <input type = button value = "delete this row" onclick = "del (this)"> </td>
</Tr>
<Tr>
<Td> cell 3 </td>
<Td> <input type = button value = "delete this row" onclick = "del (this)"> </td>
</Tr>
</Table>
</Body>

Add row
Copy codeThe Code is as follows:
<Script>

Function uf_test (){
// Obtain the table object
Var tbTable = document. getElementById ("tb_test ");
// Insert a row
Var trT = tbTable. insertRow ();

// Get the number of td in the previous row
Var nRows = tbTable. rows [0]. cells. length;

// Insert td cyclically based on the number of td in the previous row
For (var I = 0; I <nRows; I ++ ){
// Create a td object
Var tdT = document. createElement ("TD ");
// Assign a value to the td object
// TdT. innerHTML = "sfsdf ";
TdT. innerText = "sfsdf ";
// Add td to tr
TrT. appendChild (tdT );
}
}

</Script>

<Table id = "tb_test" border = "1">
<Tr>
<Td> <input type = "text"> </td>
</Tr>
</Table>

<Input type = "button" onclick = "uf_test ();" value = "add a row">

Related Article

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.