1、一般做法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>li的排序(call)</title>
<meta name="" content="" />
</head>
<body>
<ul id="ul1">
<li>78</li>
<li>92</li>
<li>69</li>
<li>54</li>
<li>70</li>
<li>48</li>
</ul>
<script type="text/javascript">
var eles = document.getElementsByTagName("li");
var oUl = document.getElementsByTagName("ul").item(0);
(function(){
var a = [];
for(var i=0;i<eles.length;i++){
a[i] = eles[i];
}
a.sort(function(li1,li2){
var n = parseFloat(li1.innerHTML);
var m = parseFloat(li2.innerHTML);
return n-m;
}); for(var i=0;i<a.length;i++){
oUl.appendChild(a[i]);
}
})(); </script>
</body>
</html>
2、利用call方法,让arguments转换成数组
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>li的排序(call)</title>
<meta name="" content="" />
</head>
<body>
<ul id="ul1">
<li>78</li>
<li>92</li>
<li>69</li>
<li>54</li>
<li>70</li>
<li>48</li>
</ul> <script type="text/javascript">
var eles = document.getElementsByTagName("li");
var oUl = document.getElementsByTagName("ul").item(0);
var a = [].slice.call(eles,0);
a.sort(function(a,b){
return a.innerHTML-b.innerHTML;
})
for(var i=0;i<a.length;i++){
oUl.appendChild(a[i]);
} </script>
</body>
</html>