js实现表格单列按字母排序
程序员文章站
2022-03-20 18:48:15
本文实例为大家分享了js实现表格单列按字母排序的具体代码,供大家参考,具体内容如下类似于列表按字母排序,直接上代码啦~
本文实例为大家分享了js实现表格单列按字母排序的具体代码,供大家参考,具体内容如下
类似于列表按字母排序,直接上代码啦~
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>表格单列字母排序</title> <link href="../css/表格数据搜索.css" rel="stylesheet"> </head> <body> <input type="button" id="myinput" onclick="myfunction()" value="点击排序" style="background-image: none"> <table id="mytable"> <tr> <th>名称</th> <th>城市</th> </tr> <tr> <td>berglunds snabbkop</td> <td>sweden</td> </tr> <tr> <td>north/south</td> <td>uk</td> </tr> <tr> <td>alfreds futterkiste</td> <td>germany</td> </tr> <tr> <td>koniglich essen </td> <td>germany</td> </tr> <tr> <td>magazzini alimentari riuniti</td> <td>italy</td> </tr> <tr> <td>paris specialites</td> <td>france</td> </tr> <tr> <td>island trading</td> <td>uk</td> </tr> <tr> <td>laughing bacchus winecellars</td> <td>canada</td> </tr> </table> <script src="../js/表单单列字母排序.js"> </script> </body> </html>
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>表格数据搜索</title> <link href="../css/表格数据搜索.css" rel="stylesheet"> </head> <body> <input type="text" placeholder="搜索..." id="myinput" οnkeyup="myfunction()"> <table id="mytable"> <tr> <th>名称</th> <th>城市</th> </tr> <tr> <td>alfreds futterkiste</td> <td>germany</td> </tr> <tr> <td>berglunds snabbkop</td> <td>sweden</td> </tr> <tr> <td>island trading</td> <td>uk</td> </tr> <tr> <td>koniglich essen</td> <td>germany</td> </tr> </table> <script src="../js/表格数据搜索.js"> </script> </body> </html>
function myfunction() { var table=document.getelementbyid("mytable"); var switching=true; while(switching){ switching=false; var rows=table.getelementsbytagname("tr"); for(var i=1;i<(rows.length-1);i++){ switching=false; var x=rows[i].getelementsbytagname("td")[0]; var y=rows[i+1].getelementsbytagname("td")[0]; if (x.innerhtml.tolowercase() > y.innerhtml.tolowercase()){ switching=true; break; } } if(switching){ rows[i].parentnode.insertbefore(rows[i+1],rows[i]); switching=true; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。