2008-02-01
使用Javascript动态增加,删除表格(使用W3C对象模型)
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>

<script language="javascript">
function deleteRow(index){
var tableObj=document.getElementById("mainBody");
var rowObj=document.getElementById('row'+index);
tableObj.removeChild(rowObj);
}
function addRow(){
var tableObj=document.getElementById("mytable");
var tableBodyObj=document.getElementById("mainBody");
var newRowObj=document.createElement("tr");
newRowObj.id="row"+(tableObj.rows.length-1);
var newColName=document.createElement("td");
var newColAge=document.createElement("td");
var newColButton=document.createElement("td");
newColName.innerHTML=document.getElementById("newName").value;
newColAge.innerHTML=document.getElementById("newAge").value;
newColButton.innerHTML='<input type="button" value="删除" onclick="deleteRow('+(tableObj.rows.length-1)+')">';
newRowObj.appendChild(newColName);
newRowObj.appendChild(newColAge);
newRowObj.appendChild(newColButton);
tableBodyObj.appendChild(newRowObj);
}
</script>
</head>
<body>
<table width="100%" id="mytable" border="0" cellspacing="0" cellpadding="0">
<tbody id="mainBody">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>操作</td>
</tr>
<tr id="row0">
<td>gaoxiang</td>
<td>28</td>
<td><input type="button" onclick="deleteRow(0)" value="删除"/></td>
</tr>
<tr id="row1">
<td>gaoxiang</td>
<td>28</td>
<td><input type="button" onclick="deleteRow(1)" value="删除"/></td>
</tr>
</tbody>
</table>
<input type="text" name="name" id="newName" />
<input type="text" name="age" id="newAge"/>
<input type="button" onclick="addRow();" value="新增"/>
</body>
</html>
发表评论
- 浏览: 84453 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
Static初始化代码快访问st ...
当然啦,final型常量只能被赋值一次
-- by Chihom -
Tomcat 5.5不能使用EL表达 ...
請在web.xml修改XSD<?xml version="1.0" enc ...
-- by trowa -
一个通用的泛型GenericH ...
谢谢!使用中!
-- by sangood -
Spring AOP四种创建通知( ...
我一直想知道如何拦截某个类中特定的方法,而不是所有的方法?
-- by yourgame -
HashTable和HashMap的区 ...
这么多总结,还算你的这个不错,谢谢,学习
-- by lysmart_8






评论排行榜