-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-remove-div.html
56 lines (53 loc) · 1.16 KB
/
js-remove-div.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{margin: 0;padding: 0;}
#box{
width: 800px;
height: 400px;
background: #f39f3e;
}
.snow{
width: 100px;
height: 100px;
background: #323233;
}
</style>
<script type="text/javascript">
window.onload = function() {
var box = document.getElementById("box");
var btnAdd = document.getElementById("add");
var btnRemove = document.getElementById("remove");
var flag = false;
btnAdd.onclick = function() {
if (flag === false) {
var oAdd = document.createElement("div");
oAdd.className = "snow";
oAdd.id = "live";
box.appendChild(oAdd);
flag = true;
} else {
alert("只能有一个Div");
}
};
btnRemove.onclick = function() {
if (flag === true) {
var liveDiv = document.getElementById("live");
liveDiv.parentNode.removeChild(liveDiv);
flag = false;
} else {
alert("已经没有Div了");
}
}
}
</script>
</head>
<body>
<div id="box"></div>
<button id="add">点我增加Div</button>
<button id="remove">点我删除Div</button>
</body>
</html>