日期:2014-05-16 浏览次数:20341 次
<!doctype html> <html> <head> <meta charset="gb2312" /> <title></title> <style> </style> </head> <body> <ul id="ul"> <li>小王<em>74</em></li> <li>小李<em>65</em></li> <li>小张<em>56</em></li> <li>小周<em>84</em></li> <li>小艾<em>63</em></li> </ul> <script> function $(o){return document.getElementById(o)} function makeArray( obj ){ var arr = []; for ( var i = 0, len = obj.length; i < len; i++ ){ arr.push(obj[i]); } return arr; } function asc( x, y ){ //升序 return parseInt( x.innerHTML ) - parseInt( y.innerHTML ); } function makeOrder( wrapper, obj, order ){ var order = order || 'asc'; var wrapper = $( wrapper ); var obj = wrapper.getElementsByTagName( obj ); var arr; arr = makeArray( obj ); // 将对象转为数组 if ( order == 'asc' ){ arr.sort( asc ); } else { arr.sort( asc ).reverse(); } for( var i = 0, len = arr.length; i < len; i++ ){ wrapper.appendChild( arr[i].parentNode ) } } makeOrder( 'ul', 'em', 'asc' ) </script> </body> </html>
------解决方案--------------------
<ul id="ul"> <li>小王<em>74</em></li> <li>小李<em>65</em></li> <li>小张<em>56</em></li> <li>小周<em>84</em></li> <li>小艾<em>63</em></li> </ul> <input type="button" value="TEST" onclick="sortTable()"> <script type=text/javascript> function sortTable() { var obj = document.getElementById("ul"); var arr = obj.innerHTML.match(/<li.*?\/em>/gi); //注意:IE浏览器innerHTML会遗漏n-1个“</li>”标签 arr.sort(function(a, b) { var reg = function(o){return o.replace(/(?![^>]+(?=<\/)).+?/g, "")}; return reg(b) - reg(a) }); obj.innerHTML = arr.join("</li>") + "</li>"; } </script>
------解决方案--------------------
var reg = reg || function(o){return o.replace(/(?![^>]+(?=<\/)).+?/g, "")};
------解决方案--------------------
<!DOCTYPE html> <html> <head> <title>Test</title> <style> table{ width: 600px; border-collapse:collapse; text-align: center; } table,th, td{ border: 1px solid