标签:blog http width io 2014 for
基本思路:appendChild方法实质上是将原来存在的元素remove掉,然后在添加到后面去。
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title></title> <script> window.onload=function () { var oTab=document.getElementById('tab1'); var oBtn=document.getElementById('btn1'); oBtn.onclick=function () { var arr=[]; for(var i=0;i<oTab.tBodies[0].rows.length;i++) { arr[i]=oTab.tBodies[0].rows[i]; } arr.sort(function (tr1,tr2){ var n1=parseInt(tr1.cells[0].innerHTML); var n2=parseInt(tr2.cells[0].innerHTML); return n1-n2; }); for(var i=0;i<arr.length;i++) { oTab.tBodies[0].appendChild(arr[i]); } }; }; </script> </head> <body> <input id="btn1" type="button" value="排序"/> <table id="tab1" border="1" width="500"> <thead> <td>ID</td> <td>姓名</td> <td>年龄</td> </thead> <tbody> <tr> <td>2</td> <td>XXX</td> <td>25</td> </tr> <tr> <td>5</td> <td>王五</td> <td>29</td> </tr> <tr> <td>4</td> <td>李四</td> <td>26</td> </tr> <tr> <td>3</td> <td>SSS</td> <td>24</td> </tr> <tr> <td>1</td> <td>Xuan</td> <td>23</td> </tr> </tbody> </table> </body> </html>
效果图:
(35)DOM应用之表格中的内容排序,布布扣,bubuko.com
标签:blog http width io 2014 for
原文地址:http://blog.csdn.net/xuan6251237011/article/details/38025653