forked from Chuyue0/javascript-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom_add&delete.html
76 lines (76 loc) · 2.31 KB
/
dom_add&delete.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>javascript模拟添加和删除表格行</title>
<style type="text/css">
#table{width: 500px;}
#table td{height: 30px;}
#table,#table td{border: 1px solid #333; border-collapse: collapse;}
</style>
</head>
<body>
<table border="1" width="500" id="table" align="center">
<tr>
<th>学号</th>
<th>姓名</th>
<th>操作</th>
</tr>
<tr>
<td>xh001</td>
<td>王小明</td>
<td><a href="javascript:;" onclick="del(this)">删除</a></td>
</tr>
<tr>
<td>xh002</td>
<td>刘小芳</td>
<td><a href="javascript:;" onclick="del(this)">删除</a></td>
</tr>
</table>
<br />
<input type="button" value="添加一行" onclick="addTr()" />
<script type="text/javascript">
window.onload = function(){
// 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。
var tr=document.getElementsByTagName("tr");
for(var i=0;i<tr.length;i++){
addBg(tr[i]);
}
}
function addBg(obj){
obj.onmouseover=function(){
obj.style.backgroundColor="#f2f2f2";
}
obj.onmouseout=function(){
obj.style.backgroundColor="#fff";
}
}
function createElem(elem){
return document.createElement(elem);
}
// 编写一个函数,供添加按钮调用,动态在表格的最后一行添加子节点;
var num=document.getElementsByTagName("tr").length;
function addTr(){
var table=document.getElementById("table");
var tr=createElem("tr"),td1=createElem("td"),td2=createElem("td"),td3=createElem("td");
td1.innerHTML="xh00"+num;
td2.innerHTML=num+"号学生,无姓名";
td3.innerHTML="<a href='javascript:;' onclick='del(this)'>删除</a>";
table.appendChild(tr);
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
var tr=document.getElementsByTagName("tr");
for(var i=0;i<tr;i++){
addBg(tr[i]);
}
num++;
}
// 创建删除函数
function del(obj){
var tr=obj.parentNode.parentNode;
tr.parentNode.removeChild(tr);
}
</script>
</body>
</html>