js动态给table添加/删除tr的方法

js动态给table添加/删除tr的方法。

  1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2 <HTML>
  3 <HEAD>
  4 <TITLE>JS动态为table添加与删除tr-www.jbxue.com-脚本学堂 </TITLE>
  5 <META NAME="Generator" CONTENT="EditPlus">
  6 <META NAME="Author" CONTENT="">
  7 <META NAME="Keywords" CONTENT="">
  8 <META NAME="Description" CONTENT="">
  9 <script language="javascript">// Example: obj = findObj("image1");
 10 function findObj(theObj, theDoc)
 11 { 
 12 var p, i, foundObj; 
 13 if(!theDoc) theDoc = document; 
 14 if( (p = theObj.indexOf("?")) > 0 && parent.frames.length) 
 15 { theDoc = parent.frames[theObj.substring(p+1)].document; theObj = theObj.substring(0,p); } if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj]; for (i=0; !foundObj && i < theDoc.forms.length; i++) foundObj = theDoc.forms[i][theObj]; for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) foundObj = findObj(theObj,theDoc.layers[i].document); if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj); return foundObj;
 16 }
 17 //添加一个参与人填写行
 18 function AddSignRow(){ //读取最后一行的行号,存放在txtTRLastIndex文本框中 
 19 var txtTRLastIndex = findObj("txtTRLastIndex",document);
 20 var rowID = parseInt(txtTRLastIndex.value);
 21 var signFrame = findObj("SignFrame",document);
 22 //添加行
 23 var newTR = signFrame.insertRow(signFrame.rows.length);
 24 newTR.id = "SignItem" + rowID;
 25 //添加列:序号
 26 var newNameTD=newTR.insertCell(0);
 27 //添加列内容
 28 newNameTD.innerHTML = newTR.rowIndex.toString();
 29 //添加列:姓名
 30 var newNameTD=newTR.insertCell(1);
 31 //添加列内容
 32 newNameTD.innerHTML = "<input name='txtName" + rowID + "' id='txtName" + rowID + "' type='text' size='12' />";
 33 //添加列:电子邮箱
 34 var newEmailTD=newTR.insertCell(2);
 35 //添加列内容
 36 newEmailTD.innerHTML = "<input name='txtEMail" + rowID + "' id='txtEmail" + rowID + "' type='text' size='20' />";
 37 //添加列:电话
 38 var newTelTD=newTR.insertCell(3);
 39 //添加列内容
 40 newTelTD.innerHTML = "<input name='txtTel" + rowID + "' id='txtTel" + rowID + "' type='text' size='10' />";
 41 //添加列:手机
 42 var newMobileTD=newTR.insertCell(4);
 43 //添加列内容
 44 newMobileTD.innerHTML = "<input name='txtMobile" + rowID + "' id='txtMobile" + rowID + "' type='text' size='12' />";
 45 
 46 //添加列:公司名
 47 var newCompanyTD=newTR.insertCell(5);
 48 //添加列内容
 49 newCompanyTD.innerHTML = "<input name='txtCompany" + rowID + "' id='txtCompany" + rowID + "' type='text' size='20' />";
 50 //添加列:删除按钮
 51 var newDeleteTD=newTR.insertCell(6);
 52 //添加列内容
 53 newDeleteTD.innerHTML = "<div align='center' style='width:40px'><a href='#'" + rowID + "')">删除</a></div>";
 54 //将行号推进下一行
 55 txtTRLastIndex.value = (rowID + 1).toString() ;
 56 }
 57 //删除指定行
 58 function DeleteSignRow(rowid){
 59 var signFrame = findObj("SignFrame",document);
 60 var signItem = findObj(rowid,document);
 61 //获取将要删除的行的Index
 62 var rowIndex = signItem.rowIndex;
 63 //删除指定Index的行
 64 signFrame.deleteRow(rowIndex);
 65 //重新排列序号,如果没有序号,这一步省略
 66 for(i=rowIndex;i<signFrame.rows.length;i++){
 67 signFrame.rows[i].cells[0].innerHTML = i.toString();
 68 }
 69 }//清空列表
 70 function ClearAllSign(){
 71 if(confirm('确定要清空所有参与人吗?')){
 72 var signFrame = findObj("SignFrame",document);
 73 var rowscount = signFrame.rows.length;
 74 //循环删除行,从最后一行往前删除
 75 for(i=rowscount - 1;i > 0; i--){
 76 signFrame.deleteRow(i);
 77 }
 78 //重置最后行号为1
 79 var txtTRLastIndex = findObj("txtTRLastIndex",document);
 80 txtTRLastIndex.value = "1";
 81 //预添加一行
 82 AddSignRow();
 83 }
 84 }
 85 </script>
 86 </HEAD>
 87 <BODY>
 88 <div>
 89 <table width="613" border="0" cellpadding="2" cellspacing="1" id="SignFrame">
 90 <tr id="trHeader">
 91 <td width="27" bgcolor="#96E0E2">序号</td>
 92 <td width="64" bgcolor="#96E0E2">用户姓名</td>
 93 <td width="98" bgcolor="#96E0E2">电子邮箱</td>
 94 <td width="92" bgcolor="#96E0E2">固定电话</td>
 95 <td width="86" bgcolor="#96E0E2">移动手机</td>
 96 <td width="153" bgcolor="#96E0E2">公司名称</td>
 97 <td width="57" align="center" bgcolor="#96E0E2"> </td>
 98 </tr>
 99 </table>
100 </div>
101 <div>
102 <input type="button" name="Submit" value="添加参与人" /> 
103 <input type="button" name="Submit2" value="清空" />
104 <input name='txtTRLastIndex' type='hidden' id='txtTRLastIndex' value="1" />
105 </div>
106 </BODY>
107 </HTML>
posted @ 2013-08-12 05:53  un123  阅读(1680)  评论(0编辑  收藏  举报