Js動態添加刪除一行

<html>
<head>
<title>動態加載一行</title>
<script language="javascript">
function $(id)
{
return document.getElementById(id);
}

function addRow()
{
var tableObj = $("testTable");
var rowObj = tableObj.insertRow(-1);
rowObj.setAttribute("id","newRow");
var cellObj1 = rowObj.insertCell(-1);
var cellObj2 = rowObj.insertCell(-1);
cellObj1.innerHTML = '<input type="text" value="A"/>';
cellObj2.innerHTML ='<input type="button" value="添加" onclick="addRow()"/>'
+ '<input type="button" value="刪除" onclick="delRow(1)"/>';

}
function delRow(obj)
{
var tableObj = $("testTable");
if(1==obj)
{
tableObj.deleteRow(-1);
}

}
</script>
</head>
<body>
<table id="testTable" border="1">
<tr>
<td><input type="text" value="A"/></td>
<td>
<input type="button" value="添加" onclick="addRow()"/>
<input type="button" value="刪除" onclick="delRow()"/>
</td>
</tr>
</table>
</body>
</html>

 

本文出自“my-imagination”
 

You May Also Like