今天要實(shí)現(xiàn)一個(gè)在頁(yè)面中動(dòng)態(tài)添加以及刪除一行列表的功能,查找了幾種方法,在此備份,以便日后使用:
========================此方法比較簡(jiǎn)潔,,而且可以解決問(wèn)題======================== function deleteCurrentRow()//刪除當(dāng)前行 { var currRowIndex=event.srcElement.parentNode.parentNode.rowIndex; document.all.table10.deleteRow(currRowIndex);//table10--表格id }
function insertRow() { var nRow=document.all.table10.rows.length; //表格的總行數(shù) var objTheRow=document.all.table10.insertRow(nRow);//在最下邊新增一行 objTheRow.insertCell(0);//新增一個(gè)單元格 objTheRow.insertCell(1); objTheRow.insertCell(2); objTheRow.cells(0).innerHTML=nRow;//對(duì)新增的單元格?容 objTheRow.cells(1).innerHTML=" "; objTheRow.cells(2).innerHTML="<input type='button' value='del this row' onClick='deleteCurrentRow()'>"; }
====================我的程序代碼====================== <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=GB18030"> <META name="GENERATOR" content="IBM WebSphere Studio"> <TITLE>cfbcard.html</TITLE> </HEAD>
<SCRIPT language="JavaScript"> var j_1 = 1; function add_row_family(){ newRow=document.all.family.insertRow(-1) newcell=newRow.insertCell() newRow.bgColor='#FFFFFF'; newcell.className='STYLE3'; newcell.align='center'; //newcell.innerHTML="<input type='text' name='familyname"+j_1+"' style='WIDTH: 60px; font-size:9pt; color:#000000' />"; newcell.innerHTML="<SELECT name='thesistogether"+j_1+"'>"+ " <option value='請(qǐng)選擇'>"+ " 請(qǐng)選擇"+ " </option>"+ " <option value='1'>"+ " 111"+ " </option>"+ " <option value='2'>"+ " 222"+ " </option>"+ " <option value='3'>"+ " 333"+ " </option>"+ " <option value='4'>"+ " 444"+ " </option>"+ " <option value='5'>"+ " 555"+ " </option>"+ "</SELECT>"; for(var i = 0;i<12;i++){ newcell=newRow.insertCell() ; newRow.bgColor='#FFFFFF'; newcell.className='STYLE3'; newcell.align='center'; newcell.innerHTML="<input type='text' name='familyrelation"+j_1+"' style='WIDTH: 60px; font-size:9pt; color:#000000' />"; } newcell=newRow.insertCell() ; newRow.bgColor='#FFFFFF'; newcell.className='STYLE3'; newcell.align='center'; //newcell.innerHTML="<a href='javascript:delTableRow(\""+1+"\")'>刪除</a>"; newcell.innerHTML="<input type='button' value='刪除' onClick='deleteCurrentRow()'>";
j_1++; document.all.j_1.value=j_1; document.all.family.focus(); }
function deleteCurrentRow()//刪除當(dāng)前行 { var currRowIndex=event.srcElement.parentNode.parentNode.rowIndex; document.all.family.deleteRow(currRowIndex);//table10--表格id }
</script>
<body bgcolor="#F5F1F5" >
<form name="form1" method="post" action="" onsubmit=""> <table> <tr> <td align="right"><INPUT type="button" name="add" onclick="add_row_family();" value="添加"></td> </tr> <tr> <td> <table id="family" style="width:100%" border="1" cellspacing="1" cellpadding="2" class="tbMain"> <tr> <td class="td_name">111</td> <td class="td_name">222</td> <td class="td_name">333</td> <td class="td_name">444</td> <td class="td_name">555</td> <td class="td_name">666</td> <td class="td_name">777</td> <td class="td_name">888</td> <td class="td_name">999</td> <td class="td_name">000</td> <td class="td_name">123</td> <td class="td_name">456</td> <td class="td_name">789</td> <td class="td_name">刪除</td> </tr> </table> </td> </tr> </table> </form> </body> </html>
=================================另外一種方法============== 如何刪除表格的行上次講到了如何動(dòng)態(tài)給表格增加行,那么這次就講講如何刪除表格的行了,。首先建立一個(gè)表格,, <table border="1"> <tr> <td>姓名</td> <td>地址</td> </tr> <tbody id="mainbody"> <tr id="delCell"> <td>name</td> <td>address</td> </tr> </tbody> </table> 取得tbody的元素var mailbody = document.getElementById("mainbody");, 接著取得要?jiǎng)h除行的元素var cell = document.getElementById("delCell"); 最后就是從tbody中移去要?jiǎng)h除的行就可以了mainbody.removeChild(cell); 完整的代碼如下: <html> <head> <title>動(dòng)態(tài)刪除表格的行</title> <script type="text/javascript"> function deleteCell(){ var mailbody = document.getElementById("mainbody"); var cell = document.getElementById("delCell"); if(cell!=undefined){ mainbody.removeChild(cell); } } </script> </head> <body> <table border="1"> <tr> <td>姓名</td> <td>地址</td> </tr> <tbody id="mainbody"> <tr id="delCell"> <td>name</td> <td>address</td> </tr> </tbody> </table>
<input type="button" value="刪除" onclick="deleteCell()"/> </body> <html>
|