forked from xingbaofeng/course2015_7_weekend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3DOM_element_sort1.html
50 lines (47 loc) · 1 KB
/
3DOM_element_sort1.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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<ul id="ul1">
<li>78</li>
<li>67</li>
<li>88</li>
<li>91</li>
<li>66</li>
<li>62</li>
<li>99</li>
<li>64</li>
<li>83</li>
<li>77</li>
</ul>
</body>
</html>
<script>
var oLis=document.getElementsByTagName("li");
/*var obj=[{name:"rose",age:22},{name:"tom",age:18},{name:"jack",age:"33"},{name:"jerry",age:26}];
//按着每个对象的年龄进行排序
obj.sort(function(a,b){
return a.age- b.age;
})
var a=[8,6,5,7,3,10];
a.sort(function(a,b) {
return a-b;
})
*/
var a=[];
for(var i=0;i<oLis.length;i++){
a.push(oLis[i]);
}
a.sort(function(a,b){
return a.innerHTML- b.innerHTML;
});
var oUl=a[0].parentNode;
oUl.innerHTML="";
for(var i=0;i< a.length;i++){
//alert(i);
oUl.appendChild(a[i]);//按着数组里的li的顺序,重新调整文档的结构。
}
</script>