-
Notifications
You must be signed in to change notification settings - Fork 1
/
js-task-04.html
130 lines (114 loc) · 3.11 KB
/
js-task-04.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!DOCTYPE>
<html>
<head>
<meta charset="utf-8">
<title>IFE JavaScript Task 04</title>
<style type="text/css">
.num-queue span{
border: 1px solid black;
border-radius: 3px;
display: inline-block;
margin: 4px;
padding: 2px;
}
</style>
</head>
<body>
<div>
<input type='text' class='num'/>
<button class='left-in'>左侧入</button>
<button class='right-in'>右侧入</button>
<button class='left-out'>左侧出</button>
<button class='right-out'>右侧出</button>
</div>
<div class='num-queue'>
</div>
<script>
(function(){
const inputBox = document.querySelector('.num');
const leftIn = document.querySelector('.left-in');
const rightIn = document.querySelector('.right-in');
const leftOut = document.querySelector('.left-out');
const rightOut = document.querySelector('.right-out');
// 从右侧添加数字
function appendItem(){
const numQueue = document.querySelector('.num-queue');
const numContainer = document.createElement('span');
if(validateInput(inputBox)){
numContainer.textContent = inputBox.value;
numQueue.appendChild(numContainer);
};
};
// 从左侧添加数字
function insertItem(){
const numQueue = document.querySelector('.num-queue');
const numContainer = document.createElement('span');
if(validateInput(inputBox)){
numContainer.textContent = inputBox.value;
numQueue.insertBefore(numContainer, numQueue.firstChild);
};
}
// 移除数字
function removeItem(item){
const numQueue = document.querySelector('.num-queue');
if(numQueue.firstElementChild){
numQueue.removeChild(item);
alert(`队列中还剩下${numQueue.children.length}个数字。`);
} else {
alert(`队列中没有数字, 请添加。`);
}
}
// 检查数字输入合法性
function validateInput(input){
if(input.value.match(/^[0-9]+$/)){
return input.value;
} else{
input.focus();
alert('请输入正确格式(数字)!');
}
};
// 为按钮添加点击事件
function clickHandler(btn){
btn.addEventListener('click', () => {
const numQueue = document.querySelector('.num-queue');
switch (btn) {
case rightIn:
appendItem();
inputBox.value = '';
clickNumToRemove();
break;
case leftIn:
numQueue.children[0] ? insertItem() : appendItem();
inputBox.value = '';
clickNumToRemove();
break;
case rightOut:
removeItem(numQueue.lastElementChild);
break;
case leftOut:
removeItem(numQueue.firstElementChild);
break;
}
})
};
// 点击队列某个元素以删除该元素
function clickNumToRemove(){
const nums = document.querySelectorAll('.num-queue span');
nums.forEach(num => {
num.onclick = function(){
removeItem(this);
}
});
};
function init(){
clickHandler(rightIn);
clickHandler(leftIn);
clickHandler(rightOut);
clickHandler(leftOut);
clickNumToRemove();
};
init();
})();
</script>
</body>
</html>