关于JS添加table行和删除行的问题
function insertRows(){
var tempRow=0;
var tbl=document.getElementById("dictTbl");
tempRow=tbl.rows.length; //获得当前table的行数
//alert(tempRow);
var Rows=tbl.rows;//类似数组的Rows
var newRow=tbl.insertRow(tbl.rows.length);//插入新的1行
//alert(tbl.rows.length);
var Cells=newRow.cells;//类似数组的Cells
for (i=0;i<3;i++)//每行的3列数据
{
//alert(newRow.rowIndex + " " + Cells.length);
var newCell=Rows(newRow.rowIndex).insertCell(Cells.length);
newCell.align="center";
switch (i)
{
case 0 : newCell.innerHTML="<td class="ta_01" align="center" width="15%">"+tempRow+"</td>";break;
case 1 : newCell.innerHTML="<td class="ta_01" align="center" width="60%"><input name="itemname" type="text" id=""+tempRow+"" size="45" maxlength=25></td>";break;
case 2 : newCell.innerHTML="<td class="ta_01" align="center" width="25%"><a href='javascript:delTableRow(""+tempRow+"")'><img src=${pageContext.request.contextPath }/images/delete.gif width=15 height=14 border=0 style=CURSOR:hand></a></td>";break;
}
}
}
function delTableRow(rowNum){
var tbl=document.getElementById("dictTbl");
//alert(rowNum);
//alert(tbl.rows.length);
if (tbl.rows.length >rowNum){
tbl.deleteRow(rowNum);
//alert(rowNum);
//alert(tbl.rows.length);
for (i=rowNum;i<tbl.rows.length;i++)
{
tbl.rows[i].cells[0].innerText=i;
tbl.rows[i].cells[2].innerHTML="<a href='javascript:delTableRow(""+i+"")'><img src=${pageContext.request.contextPath }/images/delete.gif width=15 height=14 border=0 style=CURSOR:hand></a>";
}
}
}
------分隔线----------------------------
------分隔线----------------------------