-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path简单的DOM操作-输入和输出.html
75 lines (71 loc) · 2.01 KB
/
简单的DOM操作-输入和输出.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>IFE JavaScript 16</title>
<style type="text/css">
ul li{
float:left;
background-color: red;
color:#fff;
margin:0 20px;
padding:20px;
cursor:pointer;
}
</style>
</head>
<body>
<input type="text" id="text" placeholder="输入一个数字" />
<input type="button" id="left-add" value="左侧入" />
<input type="button" id="right-add" value="右侧入" />
<input type="button" id="left-dele" value="左侧出" />
<input type="button" id="right-dele" value="右侧出" />
<ul id="cl">
<li>20</li>
<li>16</li>
<li>5</li>
<li>55</li>
<li>0</li>
</ul>
<script type="text/javascript">
window.onload=function(){
var ul =document.getElementById("cl");
var Li = ul.children;
setInterval(
function(){
for(var i=0;i<Li.length;i++){
Li[i].onclick=function(){
ul.removeChild(this);
}
}
},50)
var leftadd=document.getElementById("left-add");
leftadd.onclick = function(){
var textType = document.getElementById("text").value;
var newLi = document.createElement("li");
newLi.innerHTML=textType;
ul.insertBefore(newLi,Li[0]);
} ;
var rigthAdd = document.getElementById("right-add");
rigthAdd.onclick = function(){
var textType = document.getElementById("text").value;
var newLi = document.createElement("li");
newLi.innerHTML=textType;
ul.insertBefore(newLi,Li[-1]);
}
var leftDele = document.getElementById("left-dele");
leftDele.onclick = function(){
var leftNum = Li[0].innerText;
alert(leftNum);
ul.removeChild(ul.firstElementChild);
}
var rightDele = document.getElementById("right-dele");
rightDele.onclick = function(){
var rightNum =ul.lastElementChild.innerText;
alert(rightNum);
ul.removeChild(ul.lastElementChild);
}
}
</script>
</body>
</html>