今天要实现一个在页面中动态添加以及删除一行列表的功能,查找了几种方法,在此备份,以便日后使用:
========================此方法比较简洁,而且可以解决问题========================
function deleteCurrentRow()//刪除當前行
{
var currRowIndex=event.srcElement.parentNode.parentNode.rowIndex;
document.all.table10.deleteRow(currRowIndex);//table10--表格id
}
function insertRow()
{
var nRow=document.all.table10.rows.length; //表格的總行數
var objTheRow=document.all.table10.insertRow(nRow);//在最下邊新增一行
objTheRow.insertCell(0);//新增一個單元格
objTheRow.insertCell(1);
objTheRow.insertCell(2);
objTheRow.cells(0).innerHTML=nRow;//對新增的單元格?容
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='请选择'>"+
" 请选择"+
" </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()//刪除當前行
{
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>
=================================另外一种方法==============
如何删除表格的行上次讲到了如何动态给表格增加行,那么这次就讲讲如何删除表格的行了。首先建立一个表格,
<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");,
接着取得要删除行的元素var cell = document.getElementById("delCell");
最后就是从tbody中移去要删除的行就可以了mainbody.removeChild(cell);
完整的代码如下:
<html>
<head>
<title>动态删除表格的行</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>
posted on 2007-04-18 23:49
阿伟 阅读(2934)
评论(2) 编辑 收藏 所属分类:
JS