js 操作 table
Code
1 <table border=1 width=400 id=tb>
2 <tr>
3 <td>firstLeft</td>
4 <td>firstRight</td>
5 </tr>
6 <tr>
7 <td>lastLeft</td>
8 <td>lastRight</td>
9 </tr>
10 </table>
11
12 <script language="JavaScript">
13 <!--
14 var n=1;
15
16 function insertRow() {
17 var row=tb.insertRow(tb.rows.length-1);
18 var cell=row.insertCell(0);
19 cell.innerHTML=n;
20 cell=row.insertCell(1);
21 cell.innerHTML="<a href='javascript://' onclick='removeOne(this)'>remove</a>";
22 n++;
23 }
24
25 function removeOne(obj) {
26 var _tr=obj.parentNode.parentNode;
27 _tr.parentNode.removeChild(_tr);
28 }
29
30 function removeAll() {
31 while(tb.rows.length>2){
32 tb.deleteRow( 1 );
33 }
34 }
35 //-->
36 </script>
37 <hr>
38
39 <input type="button" onclick='insertRow()' value=" insert ">
40 <input type="button" onclick='removeAll()' value=" remove all">
41