how2j.cn

步骤 1 : 练习-表格排序   
步骤 2 : 答案-表格排序   

步骤 1 :

练习-表格排序

练习难度
Or  姿势不对,事倍功半! 点击查看做练习的正确姿势
点击表头,实现排序效果
"); window.frames["iframe_show2943"].document.write(decodeHtml($("textarea#stepcodeTextarea2943").val())); window.frames["iframe_show2943"].document.close(); $(window.frames["iframe_show2943"]).load(function(){ $("#iframe_show2943").height($("#iframe_show2943").contents().find("body").height()+showittryitheight); }); $("#iframe_show2943").height($("#iframe_show2943").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
<style> table{ border-collapse:collapse; width:90%; } tr{ border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: lightgray; height:35px; } td{ width:25%; text-align:center; } a{ text-decoration: none; color:skyblue; } </style> <script> var col = 0; var reverse = false; function sort(column){ if(column == col) reverse = !reverse; col = column; var tbody = document.getElementById("tbody"); //通过getElementsByTagName取出来是一个Collection var trsCollection = document.getElementsByTagName("tr"); //因为Collection没有自带的排序函数,所以需要转换为数组,利用数组自带的排序 var trs =new Array(); for (var i=1; i <trsCollection.length; i++) { trs.push(trsCollection[i]); } trs.sort(comparator); for (var i=0; i <trs.length; i++) { tbody.appendChild(trs[i]); } } function comparator(tr1,tr2){ var td1 = tr1.children[col].innerHTML; //取某一行的第col列中的内容 var td2 = tr2.children[col].innerHTML; if(reverse) return td1.localeCompare(td2); else return td2.localeCompare(td1); } </script> <table> <tbody id="tbody"> <tr > <td ><a href="javascript:void(0)" onclick="sort(0)">id</a></td> <td ><a href="javascript:void(0)" onclick="sort(1)">名称</a></td> <td ><a href="javascript:void(0)" onclick="sort(2)">血量</a></td> <td ><a href="javascript:void(0)" onclick="sort(3)">伤害</a></td> </tr> <tr > <td>1</td> <td>gareen</td> <td>340</td> <td>58</td> </tr> <tr > <td>2</td> <td>teemo</td> <td>320</td> <td>76</td> </tr> <tr > <td>3</td> <td>annie</td> <td>380</td> <td>38</td> </tr> <tr > <td>4</td> <td>deadbrother</td> <td>400</td> <td>90</td> </tr> </tbody> </table>
"); window.frames["iframe2943"].document.write(decodeHtml(code2943)); window.frames["iframe2943"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe2943"]).load(function(){ $("#iframe2943").height($("#iframe2943").contents().find("body").height()+showittryitheight); }); $("#iframe2943").height($("#iframe2943").contents().find("body").height()+showittryitheight); alreadyWriteCode2943 = code2943; $("#rendering2943").hide(); $("#rendered2943").show(); } var tRereshRetry2DemoPanel2943 = setInterval(rereshRetry2DemoPanel2943,1000); var binded2943 = false; $("textarea#stepcodeTextarea2943").keyup(function(){ if(!binded2943){ $(window).bind('beforeunload',function(){ binded2943 = true; return "xxxx"; }); } var newCode = $(this).val() code2943 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code2943!=newCode){ // window.frames["iframe2943"].document.write("
"); // window.frames["iframe2943"].document.write(decodeHtml($("textarea#stepcodeTextarea2943").val())); // window.frames["iframe2943"].document.close(); // $(window.frames["iframe2943"]).load(function(){ // $("#iframe2943").height($("#iframe2943").contents().find("body").height()+showittryitheight); // }); // code2943 = newCode; // } }); $(".tryButton2943").click(function(){ $("#tryDiv2943").show(); $("#stepcodeTextarea2943").focus(); $("#stepcodeTextarea2943").height(200); $("#iframe2943").height(0); window.frames["iframe2943"].document.write("
"); window.frames["iframe2943"].document.write(decodeHtml($("textarea#stepcodeTextarea2943").val())); window.frames["iframe2943"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe2943"]).load(function(){ $("#iframe2943").height($("#iframe2943").contents().find("body").height()+showittryitheight); }); $("#iframe2943").height($("#iframe2943").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor2943.focus(); editor2943.setSize(null, "250"); $("#rendering2943").hide(); $("#rendered2943").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor2943 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea2943"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor2943.on("change",function(doc){ if(!binded2943){ $(window).bind('beforeunload',function(){ binded2943 = true; return "xxxx"; }); } var newCode = doc.getValue(); code2943 = newCode; $("textarea#stepcodeTextarea2943").val(newCode); if(alreadyWriteCode2943!=code2943){ lastModifedTime2943 = new Date().getTime(); $("#rendering2943").show(); $("#rendered2943").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor2943 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor2943.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv2943").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
步骤 2 :

答案-表格排序

在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-HTML DOM-表格排序 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图