javascript案例,动态添加和删除表格行

javascript案例,动态添加和删除表格行

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>javascript案例,动态添加和删除表格行,雪豹软件工作室</title>
<link rel="stylesheet" type="text/css" href="body.css">
<link rel="stylesheet" type="text/css" href="input2.css">
<link rel="stylesheet" type="text/css" href="mark.css">
<style type="text/css">
table {
    margin-right: auto;
    margin-left: auto;
    margin: auto;
}
</style>

<script type="text/javascript">
	var count = 0;//序号
function saveInfo(){
	count++;//每次序号自动加1
	var tableNode = document.getElementById("testTable");
	var trNode = document.createElement("tr");
	//可以使用dom方式,也可以使用innerHTML方式,以下,我2种方式都写了,dom方式我已经注释掉了,如果要用dom方式的话,解注释就可以了
	/* 
	var thNode1 = document.createElement("th");
	var thNode2 = document.createElement("th");
	var thNode3 = document.createElement("th");
	var thNode4 = document.createElement("th"); 
	*/
	
	/* 
	var userName_textNode = document.createTextNode(document.getElementById("userName").value);
	var password_textNode = document.createTextNode(document.getElementById("password").value); 
	*/
	
	var userName_Value = document.getElementById("userName").value;
	var password_Value = document.getElementById("password").value;
	
	/* 
	var aNode = document.createElement("A");
	aNode.href = "#";
	aNode.onclick = deleteMe;
	aNode.title = "我是第" + count + "行,点我可以删除本行";
	var delete_textNode = document.createTextNode("删除"); 
	*/
	
	/* 
	aNode.appendChild(delete_textNode);
	thNode1.appendChild(document.createTextNode(count));
	thNode2.appendChild(userName_textNode);
	thNode3.appendChild(password_textNode);
	thNode4.appendChild(aNode);
	trNode.appendChild(thNode1);
	trNode.appendChild(thNode2);
	trNode.appendChild(thNode3);
	trNode.appendChild(thNode4); 
	*/
	
	var aNodeInfo = '<a href="#" onclick="deleteMe2(this)" title="我是第' + count + '行,点我可以删除本行">删除</a>';
	var checkboxNodeInfo = '<input type="checkbox" name="id" value="'  + count + '">';
	var tdNode = "<td>" + checkboxNodeInfo + count +"</td><td>" + userName_Value + "</td><td>" + password_Value + "</td><td>" + aNodeInfo + "</td>";
	//alert(tdNode);
	trNode.innerHTML = tdNode;
	tableNode.appendChild(trNode);
}
	
	
	function deleteMe() {
		//alert(this);
		//alert("deleteMe");
		var myTr = this.parentNode.parentNode;
		// this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
		var isSure = confirm("您确定要删除序号为" + myTr.firstChild.firstChild.nodeValue
				+ "的数据吗?");
		if (isSure) {
			myTr.parentNode.removeChild(myTr);
		}
	}
	
	function deleteMe2(obj) {
		//alert(obj.nodeName);
		var myTr = obj.parentNode.parentNode;
		var isSure = confirm("您确定要删除序号为" + myTr.firstChild.firstChild.nodeValue
				+ "的数据吗?");
		if (isSure) {
			myTr.parentNode.removeChild(myTr);
		}
	}
</script>
</head>
<body>
	<table class="myTable" style="width:50%;">
		<tr>
			<td>用户名:</td>
			<td><input type="text"  value="江西省赣州市于都县" id="userName"></td>
			<td><div id="errorUserName"></div></td>
		</tr>
		<tr>
			<td>密码:</td>
			<td><input type="password" value="于都县" id="password"></td>
			<td><div id="errorPassword"></div></td>
		</tr>
		<tr>
			<td colspan="3">
			<input type="button" value="注册" onclick="saveInfo()">
			<input type="reset" value="删除">
			</td>
		</tr>
	</table>
	<br>
	<table class="myTable" id="testTable">
	<tr>
	<th><input type="checkbox" name="id" value="all">序号</th>
	<th>用户名</th>
	<th>密码</th>
	<th>操作</th>
	</tr>
	</table>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值