js 동적 으로 table 에 tr 를 추가 / 삭제 하 는 방법

14494 단어 table
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>

좋은 웹페이지 즐겨찾기