-
Notifications
You must be signed in to change notification settings - Fork 2
/
18.html
157 lines (157 loc) · 4.6 KB
/
18.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>task 18</title>
<style>
#tip{
height: 150px;
margin: 20px;
}
#tip>span{
display: inline-block;
width: 10px;
}
#box>span{
display: inline-block;
width: 40px;
height: 40px;
text-align: center;
line-height: 40px;
background-color: red;
color: white;
margin-right: 20px;
margin-bottom: 20px;
border-radius: 5px;
}
</style>
</head>
<body>
<form>
<input type="text" id="data"/>
<input type="button" value="左侧入" id="btn1"/>
<input type="button" value="右侧入" id="btn2"/>
<input type="button" value="左侧出" id="btn3"/>
<input type="button" value="右侧出" id="btn4"/>
<input type="button" value="排序" id="btn5">
</form>
<div id="tip"></div>
<div id="box"></div>
<script>
var number = document.getElementById('data'),
tip = document.getElementById('tip'),
box = document.getElementById('box'),
btn1 = document.getElementById('btn1'),
btn2 = document.getElementById('btn2'),
btn3 = document.getElementById('btn3'),
btn4 = document.getElementById('btn4');
var total;
//获取数据
function getData(){
var data = number.value.trim();
var len = tip.children.length;
if(data === ""){
alert("输入无效,请重新输入!");
number.focus();
}
else if ((data < 10) || (data > 100)) {
alert("请输入10-100之间的数字!");
number.value = '';
number.focus();
}else if (len > 60) {
alert("数据条目总数不能大于60!");
}else{
return data;
}
}
//创建新的数字块节点
function createBox(data){
var newNode = document.createElement('span');
newNode.innerHTML = data;
return newNode;
}
//生成随机颜色
function ranColor(){
var c1 = Math.floor(Math.random(0, 1) * 255);
var c2 = Math.floor(Math.random(0, 1) * 255);
var c3 = Math.floor(Math.random(0, 1) * 255);
var color = 'rgb(' + c1 + ',' + c2 + ',' + c3 + ')';
return color;
}
//创建新的彩色矩形
function createTip(data){
var newNode = document.createElement('span');
data += 'px';
newNode.style.height = data;
newNode.style.backgroundColor = ranColor();
return newNode;
}
//左侧入
btn1.onclick = function(){
var data = getData();
if(data !== undefined){
var newNode = createBox(data);
box.insertBefore(newNode, box.firstChild);
var newTip = createTip(data);
tip.insertBefore(newTip, tip.firstChild);
number.value = '';
number.focus();
}
};
//右侧入
btn2.onclick = function(){
var data = getData();
if(data !== undefined){
var newNode = createBox(data);
box.appendChild(newNode);
var newTip = createTip(data);
tip.appendChild(newTip);
number.value = '';
number.focus();
}
};
//左侧出
btn3.onclick = function(){
alert(box.firstChild.innerHTML + '从左侧出');
box.removeChild(box.firstChild);
tip.removeChild(tip.firstChild);
number.focus();
};
btn4.onclick = function(){
alert(box.lastChild.innerHTML + '从右侧出');
box.removeChild(box.lastChild);
tip.removeChild(tip.lastChild);
number.focus();
};
btn5.onclick = function(){
var arr = [];
var len = tip.children.length;
for(var i = 0; i < len; i++){
arr[i] = parseInt(box.childNodes.item(i).innerHTML);
}
(function(){
var temp;
for(var i = len; i >= 2; i--){
for(var j = 0; j <= i - 1; j++){
if(arr[j] > arr[j + 1]){
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
tip.innerHTML = '';
for(var k = 0; k < len; k++){
var newTip = createTip(arr[k]);
tip.appendChild(newTip);
}
box.innerHTML = '';
for(var k = 0; k < len; k++){
var newNode = createBox(arr[k]);
box.appendChild(newNode);
}
}
}
})();
};
</script>
</body>
</html>