javascript动态添加删除表格
<html>
<head>
<script language="javascript">
function deleteRow(index)
{
var tableObj=document.getElementById("mytable");
tableObj.deleteRow(tableObj.rows.length-1);
}
function addRow()
{
var tableObj=document.getElementById("mytable");
var newRowObj=tableObj.insertRow(tableObj.rows.length);
var newColName=newRowObj.insertCell(newRowObj.cells.length);
var newColAge=newRowObj.insertCell(newRowObj.cells.length);
var newColButton=newRowObj.insertCell(newRowObj.cells.length);
newColName.innerHTML=document.getElementById("newName").value;
newColAge.innerHTML=document.getElementById("newAge").value;
newColButton.innerHTML='<input type="button" onclick="deleteRow();" value="删除" >';
}
</script>
</head>
<body>
<table width="100%" id="mytable" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>操作</td>
</tr>
</table>
<input type="text" name="name" id="newName" />
<input type="text" name="age" id="newAge"/>
<input type="button" onclick="addRow();" value="新增"/>
</body>
</html>
<head>
<script language="javascript">
function deleteRow(index)
{
var tableObj=document.getElementById("mytable");
tableObj.deleteRow(tableObj.rows.length-1);
}
function addRow()
{
var tableObj=document.getElementById("mytable");
var newRowObj=tableObj.insertRow(tableObj.rows.length);
var newColName=newRowObj.insertCell(newRowObj.cells.length);
var newColAge=newRowObj.insertCell(newRowObj.cells.length);
var newColButton=newRowObj.insertCell(newRowObj.cells.length);
newColName.innerHTML=document.getElementById("newName").value;
newColAge.innerHTML=document.getElementById("newAge").value;
newColButton.innerHTML='<input type="button" onclick="deleteRow();" value="删除" >';
}
</script>
</head>
<body>
<table width="100%" id="mytable" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>操作</td>
</tr>
</table>
<input type="text" name="name" id="newName" />
<input type="text" name="age" id="newAge"/>
<input type="button" onclick="addRow();" value="新增"/>
</body>
</html>