-
Notifications
You must be signed in to change notification settings - Fork 0
/
li排序
53 lines (49 loc) · 1.18 KB
/
li排序
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<meta charset="utf-8" />
<style>
#ul1 {
background: green;
}
#ul2 {
background:yellow;
}
</style>
<script>
window.onload = function()
{
var oUl1 = document.getElementById('ul1');
//var oUl2 = document.getElementById('ul2');
var oBtn = document.getElementById('btn1');
oBtn.onclick = function () {
var aLi = oUl1.getElementsByTagName('li');
var arr = [];
for (var i = 0; i < aLi.length; i++) {
arr[i] = aLi[i];
}
arr.sort(function(li1,li2){
var n1=parseInt(li1.innerHTML);
var n2=parseInt(li2.innerHTML);
return n1-n2;
})
for (var i = 0; i < arr.length; i++) {
oUl1.appendChild(arr[i]);
}
}
};
</script>
</head>
<body>
<input id="btn1" type="button" value="排序" />
<ul id="ul1">
<li>13</li>
<li>2</li>
<li>43</li>
<li>34</li>
<li>54</li>
</ul>
</body>
</html>