<!DOCTYPE html> <html> <head> <title>js列表排序</title> <meta charset="utf-8"> </head> <style type="text/css"> </style> <script type="text/javascript"> window.onload=function(){ var oUl=document.getElementById(‘ul1‘); var oBtn=document.getElementById(‘btn1‘); oBtn.onclick=function(){ var aLi=document.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++){ oUl.appendChild(arr[i]); } } } </script> <body> <input type="button" id="btn1" value="排序" /> <hr /> <ul id="ul1"> <li>32</li> <li>25</li> <li>3</li> <li>18</li> </body> </html>
时间: 2024-11-05 17:31:59