如下代码,已有行可以删除,但是动态添加的行无法删除?怎么解决。
本人新手,请说详细一些。<!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>动态添加删除表格</title><script type="text/javascript">
//var myRowIndex;
//function $(o)
//{
// return "function" == typeof o ? (window.onload = o) : document.getElementById(o) || o;
//}////调用
//$(function(e)
// {
// $("tb1").onclick = function(e){
// var e = e || window.event;
// var o = e.srcElement || e.target;
// //alert("第"+[o.parentNode.rowIndex+1||o.parentNode.parentNode.rowIndex+1]+"行");
// myRowIndex = o.parentNode.rowIndex+1||o.parentNode.parentNode.rowIndex+1;
//
// }
// });
//--------------------------------------
var newRowIndex=2; //被插入的行索引 //插入一行
function insertRow(tableID/*被插入一行的表的ID*/)
{
//debugger;
var Rows=tableID.rows;//类似数组的Rows
var newRow=tableID.insertRow(newRowIndex/*table.rows.length*/);//插入新的一行
for (i=0;i<3;i++)//填入每列数据
{
var newCell=Rows(newRow.rowIndex).insertCell(/*Cells.length*/);
newCell.align="center";
newCell.innerHTML=Rows(newRowIndex-1).cells(i).innerHTML;
}//for end
newRowIndex++;
}
//删除一行function deleteRow (tableID, rowIndex)
{
//var table =document.all[tableID];
tableID.deleteRow(rowIndex);
newRowIndex--;//维护全局变量
}
//function deleteRow(obj)
//{
// obj.parentElement.removeChild(obj);
// newRowIndex--;//维护全局变量
//} //function delRow(table)
//{
// var elem=document.getElementById("textid").parentNode.parentNode;
// var rowIndex=elem.rowIndex;
// table.deleteRow(rowIndex);
//
//
// alert(table.rows(1).cells(0).innerHTML);//}
</script> </head>
<body>
<form action="">
<table border="0" cellspacing="0" cellpadding="0" width="98%" align="center">
<tr valign="top">
<th>
<input value="添加" type="button" onclick="insertRow(table1)" />
</th>
</tr>
</table>
<br />
<table border="1" width="98%" align="center" id="table1">
<tr>
<th>顺序号</th>
<th>项目序号</th>
<th>删除</th>
</tr>
<tr id="insertedRow" align="center" style="display:none;"> <!--隐藏这行做为模板-->
<td><input id="textid" type="text" value="button" /></td>
<td>模板</td>
<td onclick="deleteRow(table1,this.parentElement.rowIndex)">><a href="#">删除本行</a></td> </tr>
<tr id="Tr1" align="center">
<td><input id="text1" type="text" value="button" /></td>
<td>已有行</td>
<td onclick="deleteRow(table1,this.parentElement.rowIndex)">><a href="#">删除本行</a></td> </tr>
</tr>
<tr id="Tr2" align="center">
<td><input id="text2" type="text" value="button" /></td>
<td>已有行</td>
<td onclick="deleteRow(table1,this.parentElement.rowIndex)">><a href="#">删除本行</a></td> </tr>
<tr>
<td>最后一行</td>
<td>最后一行</td>
<td>最后一行</td>
</tr>
</table>
</form>
</body>
</html>
本人新手,请说详细一些。<!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>动态添加删除表格</title><script type="text/javascript">
//var myRowIndex;
//function $(o)
//{
// return "function" == typeof o ? (window.onload = o) : document.getElementById(o) || o;
//}////调用
//$(function(e)
// {
// $("tb1").onclick = function(e){
// var e = e || window.event;
// var o = e.srcElement || e.target;
// //alert("第"+[o.parentNode.rowIndex+1||o.parentNode.parentNode.rowIndex+1]+"行");
// myRowIndex = o.parentNode.rowIndex+1||o.parentNode.parentNode.rowIndex+1;
//
// }
// });
//--------------------------------------
var newRowIndex=2; //被插入的行索引 //插入一行
function insertRow(tableID/*被插入一行的表的ID*/)
{
//debugger;
var Rows=tableID.rows;//类似数组的Rows
var newRow=tableID.insertRow(newRowIndex/*table.rows.length*/);//插入新的一行
for (i=0;i<3;i++)//填入每列数据
{
var newCell=Rows(newRow.rowIndex).insertCell(/*Cells.length*/);
newCell.align="center";
newCell.innerHTML=Rows(newRowIndex-1).cells(i).innerHTML;
}//for end
newRowIndex++;
}
//删除一行function deleteRow (tableID, rowIndex)
{
//var table =document.all[tableID];
tableID.deleteRow(rowIndex);
newRowIndex--;//维护全局变量
}
//function deleteRow(obj)
//{
// obj.parentElement.removeChild(obj);
// newRowIndex--;//维护全局变量
//} //function delRow(table)
//{
// var elem=document.getElementById("textid").parentNode.parentNode;
// var rowIndex=elem.rowIndex;
// table.deleteRow(rowIndex);
//
//
// alert(table.rows(1).cells(0).innerHTML);//}
</script> </head>
<body>
<form action="">
<table border="0" cellspacing="0" cellpadding="0" width="98%" align="center">
<tr valign="top">
<th>
<input value="添加" type="button" onclick="insertRow(table1)" />
</th>
</tr>
</table>
<br />
<table border="1" width="98%" align="center" id="table1">
<tr>
<th>顺序号</th>
<th>项目序号</th>
<th>删除</th>
</tr>
<tr id="insertedRow" align="center" style="display:none;"> <!--隐藏这行做为模板-->
<td><input id="textid" type="text" value="button" /></td>
<td>模板</td>
<td onclick="deleteRow(table1,this.parentElement.rowIndex)">><a href="#">删除本行</a></td> </tr>
<tr id="Tr1" align="center">
<td><input id="text1" type="text" value="button" /></td>
<td>已有行</td>
<td onclick="deleteRow(table1,this.parentElement.rowIndex)">><a href="#">删除本行</a></td> </tr>
</tr>
<tr id="Tr2" align="center">
<td><input id="text2" type="text" value="button" /></td>
<td>已有行</td>
<td onclick="deleteRow(table1,this.parentElement.rowIndex)">><a href="#">删除本行</a></td> </tr>
<tr>
<td>最后一行</td>
<td>最后一行</td>
<td>最后一行</td>
</tr>
</table>
</form>
</body>
</html>
2.在页面上增加,删除,移动,修改一个DOM对象对象时,最好使用通用的api,例如appendChild,removeChild等方法,别使用insertCell之类的只针对特定标签的特例方法,关于JavaScript对DOM对象的操作,可以参考这个http://www.w3school.com.cn/xmldom/index.asp
3.LZ你把onclick的操作写在了td这个标签里,但是新行的td标签是通过var newCell=Rows(newRow.rowIndex).insertCell(/*Cells.length*/)这句产生的,这种方法产生的是个新的DOM对象,不包含任何事件处理先发个LZ的版本修改版的,可以解决LZ目前的问题
<!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>动态添加删除表格</title><script type="text/javascript">
//var myRowIndex;
//function $(o)
//{
// return "function" == typeof o ? (window.onload = o) : document.getElementById(o) || o;
//}////调用
//$(function(e)
// {
// $("tb1").onclick = function(e){
// var e = e || window.event;
// var o = e.srcElement || e.target;
// //alert("第"+[o.parentNode.rowIndex+1||o.parentNode.parentNode.rowIndex+1]+"行");
// myRowIndex = o.parentNode.rowIndex+1||o.parentNode.parentNode.rowIndex+1;
//
// }
// });
//--------------------------------------
var newRowIndex=2; //被插入的行索引 //插入一行
function insertRow(tableID/*被插入一行的表的ID*/)
{
//debugger;
var Rows=tableID.rows;//类似数组的Rows
var newRow=tableID.insertRow(newRowIndex);//插入新的一行
for (i=0;i<3;i++)//填入每列数据
{
var newCell=Rows(newRow.rowIndex).insertCell();
newCell.align="center";
newCell.innerHTML=Rows(newRowIndex-1).cells(i).innerHTML;
}//for end newRowIndex++;
}
//删除一行function deleteRow (tableID, rowIndex)
{
//var table =document.all[tableID];
tableID.deleteRow(rowIndex);
newRowIndex--;//维护全局变量
} </script> </head>
<body>
<form action="">
<table border="0" cellspacing="0" cellpadding="0" width="98%" align="center">
<tr valign="top">
<th>
<input value="添加" type="button" onclick="insertRow(table1)" />
</th>
</tr>
</table>
<br />
<table border="1" width="98%" align="center" id="table1">
<tr>
<th>顺序号</th>
<th>项目序号</th>
<th>删除</th>
</tr>
<tr id="insertedRow" align="center" style="display:none;"> <!--隐藏这行做为模板-->
<td><input id="textid" type="text" value="button" /></td>
<td>模板</td>
<td><a href="#" onclick="deleteRow(table1,this.parentElement.parentElement.rowIndex)">删除本行</a></td> </tr>
<tr id="Tr1" align="center">
<td><input id="text1" type="text" value="button" /></td>
<td>已有行</td>
<td><a href="#" onclick="deleteRow(table1,this.parentElement.parentElement.rowIndex)">删除本行</a></td> </tr>
</tr>
<tr id="Tr2" align="center">
<td><input id="text2" type="text" value="button" /></td>
<td>已有行</td>
<td><a href="#" onclick="deleteRow(table1,this.parentElement.parentElement.rowIndex)">删除本行</a></td> </tr>
<tr>
<td>最后一行</td>
<td>最后一行</td>
<td>最后一行</td>
</tr>
</table>
</form>
</body>
</html>