First create an html page as sort.html, and copy the following content into it Copy code The code is as follows: Untitled Document <br>.desc span{ display:none;} <br>.asc em{ display:none;} <br> script> <br></head> <br><body> <br><table width="200" border="1" cellpadding="0" cellspacing="0" sort="true" id= "mytab"> <br><thead style="cursor:pointer"> <br><tr> <br><td class="desc">ID<span>|</span> <em>-</em></td> <br><td class="desc">name<span>|</span><em>-</em>< /td> <br></tr> <br></thead> <br><tbody> <br><tr> <br><td>1</td> <br>< td>Happy</td> <br></tr> <br><tr> <br><td>3</td> <br><td>Dinner</td> <br></tr> <br><tr> <br><td>5</td> <br><td>Open</td> <br></tr> <br>< ;/tbody> <br></table> <br><table width="200" border="1" sort="true" id="mytabs"> <br><thead style="cursor :pointer"> <br><tr> <br><td class="desc">ID<span>|</span><em>-</em></td> <br><td class="desc">name<span>|</span><em>-</em></td> <br></tr> <br>< ;/thead> <br><tbody> <br><tr> <br><td>1</td> <br><td>Happy</td> <br></tr> ; <br><tr> <br><td>3</td> ><td>5</td> <br><td>Open</td> <br></tr> <br></tbody> <br></table> <br> </body> <br></html> <br><br> <br><br>New script page sort.js <br> </div> <br><br><br>Copy code<div class="codetitle"><span><a style="CURSOR: pointer" data="93868" class="copybut" id="copybut93868" onclick="doCopy('code93868')"> The code is as follows:<u><div class="codebody" id="code93868"> <br>/* <br>Table sort function<br>Event: 2012 7 24 <br>DOM node<br>If the table needs to be sorted, add sort="true" in the table attribute <br>and the id is The only and necessary one<br>This js file can be imported directly<br>Because the repository created has no comments<br><table width="200" border="1" sort="true" id="mytab" > <br><thead style="cursor:pointer"> <br><tr> <br><td class="desc">ID<span>|</span><em> ;-</em></td> <br><td class="desc">name<span>|</span><em>-</em></td> <br></tr> <br></thead> <br><tbody> <br><tr> <br><td>1</td> <br><td>Happy </td> <br></tr> <br><tr> <br><td>3</td> /tr> <br><tr> <br><td>5</td> <br><td>Open</td> <br></tr> <br></tbody> ; <br></table> <br>*/ <br>var Core = (function(window){ <br>return { <br>init: function(){ <br>Core.getTableById(); <br>Core.setHeadClick(); <br>}, <br>I: function(id){ <br>return document.getElementById(id); <br>}, <br>N: function(name){ <br>return document.getElementsByTagName(name); <br>}, <br>config: { <br>arr:[] <br>}, <br>getTableById: function(){ <br>var table_arr=Core. N("table"); <br>for(var i = 0; i < table_arr.length; i ){ <BR>if(table_arr[i].getAttribute("sort")){ <BR>Core. config.arr.push(table_arr[i].getAttribute("id")); <BR>} <BR>} <BR>//alert(Core.config.arr) <BR>}, <BR>setHeadClick: function(){ <BR>var arr = Core.config.arr; <BR>if(!arr.length) return false; <BR>for(var j = 0 ; j< arr.length;j ){ <BR>var thead = Core.I(arr[j]).getElementsByTagName("thead")[0].getElementsByTagName("td"); <BR>for(var i = 0; i<thead.length;i ){ <BR>if(thead[i].attachEvent){ <BR>thead[i].attachEvent("onclick",Core.sortList) <BR>}else{ <BR>thead[i].addEventListener("click" ,Core.sortList,false) <BR>} <BR>//if(i==0){ <BR>// thead[i].click(); <BR>// } <BR>} <BR>} <BR>}, <BR>sortList: function(e){ <BR>var index=0,arr=[],sort="asc",table=null; <BR>if(e.srcElement){ <BR>index=e.srcElement.cellIndex; <BR>sort=e.srcElement.getAttribute("sort"); <BR>e.srcElement.className=sort == "asc" ? "desc" : "asc" ; <BR>e.srcElement.setAttribute("sort", sort == "asc" ? "desc" : "asc"); <BR>table = Core.getTableId(e.srcElement) <BR>}else{ <BR>index=e.currentTarget.cellIndex; <BR>sort=e.currentTarget.getAttribute("sort"); <BR>e.currentTarget.className=sort == "asc" ? "desc" : "asc"; <BR>e.currentTarget.setAttribute("sort", sort == "asc" ? "desc" : "asc"); <BR>table = Core.getTableId(e.currentTarget) <BR>} <BR>Core .getList(table,index,arr); <BR>Core.updateList(table,sort,arr); <BR>}, <BR>getTableId: function(p){ <BR>for(var i=0,n =p;n=n.parentNode;i ){ <BR>if(i>100) break; <br>if(n.nodeName=="TABLE"){ <br>//alert(n.nodeName/* n.getAttribute("id")*/) <br>return n; <br>} <br>} <br>}, <br>getList: function (table,index,arr){ <br>var table = table.getElementsByTagName("tbody")[0]; <br>for(var i = 0; i< table.rows.length; i ){ <BR>var item = table.rows[i]; <BR>for (var j = 0; j< item.cells.length;j ){ <BR>var jtem = item.cells[index]; <BR>if(jtem.innerHTML){ <BR>//alert(jtem.innerHTML ) <BR>arr[i]=jtem.innerHTML; <BR>} <BR>break; <BR>} <BR>} <BR>}, <BR>updateList: function (table,sort,arr){ <BR>var table = table.getElementsByTagName("tbody")[0]; <BR>for(var i = 0; i< arr.length;i ){ <BR>for(var j = i 1;j< arr .length;j ){ <BR>if(sort=="asc"){ <BR>if(arr[i] > arr[j]){ <br>var rwos=table.rows[i].cloneNode (true); <br>table.replaceChild(table.rows[j],table.rows[i]); <br>if(j 1==arr.length){ <br>//table.insertBefore(rwos ,null); <br>table.appendChild(rwos) <br>}else{ <br>table.insertBefore(rwos,table.rows[j]); <br>} <br>var tim= arr[i] ; <br>arr.splice(i,1,arr[j]); <br>arr.splice(j,1,tim); <br>} <br>}else{ <br>if(arr[i ] < arr[j]){ <br>var rwos=table.rows[i].cloneNode(true); <br>table.replaceChild(table.rows[j],table.rows[i]); <br>if(j 1==arr.length){ <br>//table.insertBefore(rwos,null); <br>table.appendChild(rwos) <br>}else{ <br>table.insertBefore(rwos ,table.rows[j]); <br>} <br>var tim= arr[i]; <br>arr.splice(i,1,arr[j]); <br>arr.splice(j, 1,tim); <br>} <br>} <br>} <br>} <br>} <br>}; <br>})(window); <br>window.onload=Core.init; <br><br> </div></u></a></span></div>