html <ul id="ul1"> <li>9</li> <li>2</li> <li>7</li> <li>4</li> <li>11</li> </ul> <input type="button" value="排序" id="btn1" />
js代码 window.onload = function(){ var oUl = document.getElementById("ul1"); var oBtn = document.getElementById("btn1"); oBtn.onclick = function(){ var arr = []; for(var i=0; i<oUl.children.length; i++){ arr.push(oUl.children[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]); } } }
li排序
时间: 2024-12-30 12:58:34