<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>Javascript动态添加表格的行和列</title> <script language="JavaScript"> var Count=false,NO=1; function addRow(){ Count=!Count; //添加一行 var newTr = testTbl.insertRow(testTbl.rows.length); //添加两列 var newTd0 = newTr.insertCell(); var newTd1 = newTr.insertCell(); var newTd2 = newTr.insertCell(); //设置列内容和属性 if(Count){newTr.style.background="#FFE1FF";} else {newTr.style.background="#FFEFD5";} newTd0.innerHTML = '<input type=checkbox id="box4">'; NO++ newTd1.innerText="第"+ NO+"行"; } </script> </head><body> <table width="399" border=0 cellspacing="1" id="testTbl" style="font-size:14px;" > <tr bgcolor="#FFEFD5"> <td width=6%><input type=checkbox id="box1"></td> <td >第1行</td> <td > </td> </tr> </table> <label> <input type="button" value="插入行" onclick="addRow()" /> </label> </body> </html> |