javascript动态增加、删除、填充表格内容
1<HTML>
2<SCRIPT LANGUAGE="JScript">
3function numberCells()
4{
5 var count=0;
6 for (i=0; i < document.all.mytable.rows.length; i++)
7 {
8 for (j=0; j < document.all.mytable.rows(i).cells.length; j++)
9 {
10 document.all.mytable.rows(i).cells(j).innerText = count;
11 count++;
12 }
13 }
14}
15
16function tb_addnew()
17{
18 var
19 ls_t=document.all("mytable")
20 maxcell=ls_t.rows(0).cells.length;
21 mynewrow = ls_t.insertRow();
22
23 for(i=0;i<maxcell;i++)
24 {
25 mynewcell=mynewrow.insertCell();
26 mynewcell.innerText="a"+i;
27
28 }
29}
30
31function tb_delete()
32{
33 var ls_t=document.all("mytable");
34 maxcell=ls_t.rows.length;
35
36 if(maxcell > 1)
37 {
38 ls_t.deleteRow() ;
39 }
40}
41
42</SCRIPT>
43<BODY onload="numberCells()">
44<TABLE id=mytable border=1>
45<TR><TH> </TH><TH> </TH><TH> </TH><TH> </TH></TR>
46<TR><TD> </TD><TD> </TD><TD> </TD><TD> </TD></TR>
47<TR><TD> </TD><TD> </TD><TD> </TD><TD> </TD></TR>
48</TABLE>
49<input type=button value="Ins Row" onclick="tb_addnew()">
50<input type=button value="Del Row" onclick="tb_delete()" >
51</BODY>
52</HTML>
53
54
55
56
2<SCRIPT LANGUAGE="JScript">
3function numberCells()
4{
5 var count=0;
6 for (i=0; i < document.all.mytable.rows.length; i++)
7 {
8 for (j=0; j < document.all.mytable.rows(i).cells.length; j++)
9 {
10 document.all.mytable.rows(i).cells(j).innerText = count;
11 count++;
12 }
13 }
14}
15
16function tb_addnew()
17{
18 var
19 ls_t=document.all("mytable")
20 maxcell=ls_t.rows(0).cells.length;
21 mynewrow = ls_t.insertRow();
22
23 for(i=0;i<maxcell;i++)
24 {
25 mynewcell=mynewrow.insertCell();
26 mynewcell.innerText="a"+i;
27
28 }
29}
30
31function tb_delete()
32{
33 var ls_t=document.all("mytable");
34 maxcell=ls_t.rows.length;
35
36 if(maxcell > 1)
37 {
38 ls_t.deleteRow() ;
39 }
40}
41
42</SCRIPT>
43<BODY onload="numberCells()">
44<TABLE id=mytable border=1>
45<TR><TH> </TH><TH> </TH><TH> </TH><TH> </TH></TR>
46<TR><TD> </TD><TD> </TD><TD> </TD><TD> </TD></TR>
47<TR><TD> </TD><TD> </TD><TD> </TD><TD> </TD></TR>
48</TABLE>
49<input type=button value="Ins Row" onclick="tb_addnew()">
50<input type=button value="Del Row" onclick="tb_delete()" >
51</BODY>
52</HTML>
53
54
55
56
我来自:向东博客