-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestDOM.html
80 lines (68 loc) · 1.91 KB
/
TestDOM.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
77
78
79
80
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Shopping list example</title>
<style>
li {
margin-bottom: 10px;
}
li button {
font-size: 8px;
margin-left: 20px;
color: #666;
}
</style>
</head>
<body>
<h1>My shopping list</h1>
<div>
<label for="item">Enter a new item:</label>
<input type="text" name="item" id="item">
<button>Add item</button>
</div>
<ul>
</ul>
<script>
let ul =document.querySelector('ul');
let inPut = document.querySelector('input');
let buTton = document.querySelector('button');
let div = document.querySelector('div');
let count=0;
function testButton() {
let goods = inPut.value+'';
if(goods===''){
alert("不能为空");
}else{
inPut.value='';
let span = document.createElement('span');
let button = document.createElement('button');
let li = document.createElement('li');
count++;
let deleAll=document.createElement('button');
ul.appendChild(li);
li.appendChild(span);
li.appendChild(button);
button.textContent='Delete';
span.textContent=goods+'';
//Button.addEventListener('click',Button.parentNode.remove(li));
if(count===1){
div.appendChild(deleAll);
deleAll.textContent='一键清空';
}
button.onclick=function(e){
ul.removeChild(li);
};
deleAll.onclick=function(e) {
const deAll=document.querySelectorAll('ul > li');
for(i=0;i<deAll.length;i++){
ul.removeChild(deAll[i]);
}
};
inPut.focus();
}
}
buTton.addEventListener('click', testButton);
</script>
</body>
</html>