日期:2014-05-16  浏览次数:20432 次

js 动态添加删除行
项目中用到了行的动态添加,删除,google一下,没找到合适的,然后看下了文档,自己写了一个。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
	<table border="1" id="mytb">
	</table>
	<input type="button" value="add" onclick="add()">&nbsp;&nbsp;&nbsp;
	<input type="button" value="del()" onclick="del()">&nbsp;&nbsp;
 </BODY>
</HTML>
<script>
var i=0;
function add(){
	var mytb=document.getElementById("mytb");
	var newRow = mytb.insertRow();
	i++;
	var newTd0 = newRow.insertCell();
    var newTd1 = newRow.insertCell();
	var newTd2 = newRow.insertCell();
    newTd0.innerHTML="<input type='checkbox' name='chbox'>";
	newTd1.innerHTML="张三"+i;
	newTd2.innerHTML="李四"+i;
}
function del(){
	var chboxs = document.getElementsByName("chbox");
	var mytb=document.getElementById("mytb");
	for(var i=chboxs.length;i>0;i--){
		var e=chboxs[i-1];
		if(e.checked){
			//行的下标是从0开始的
			mytb.deleteRow(i-1);
		}
	}
}
</script>

?