富贵资源网 Design By www.hznty.com
1、获取元素 2、获取数据 3、绑定数据 4、隔行换色 5、表格排序
<table cellpadding="0" cellspacing="0" id="tab"> <thead> <tr> <th class="cursor">姓名</th> <th class="cursor">年龄</th> <th class="cursor">分数</th> <th>性别</th> </tr> </thead> <tbody> <!--<tr class="bg0"> <td>赵老大</td> <td>45</td> <td>89</td> <td>0</td> </tr> <tr class="bg1"> <td>赵老大</td> <td>45</td> <td>89</td> <td>0</td> </tr>--> </tbody> </table> <script type="text/javascript" src="/UploadFiles/2021-04-02/utils.js">css 样式
*{ margin:0; padding:0; } table{ width: 800px; margin:50px auto; box-shadow: 0 0 10px #333; border-radius: 10px; overflow: hidden; font-size: 18px; } table thead tr{ text-align: center; width: 100%; height: 50px; background:#ffb4b4; } table thead th{ width: 200px; line-height: 50px; color: white; } table tbody tr{ height: 40px; line-height: 40px; text-align: center; } table tbody tr.bg0{ background: lightgoldenrodyellow; } table tbody tr.bg1{ background: lightgray; } .cursor{ cursor: pointer; }JS
所用到的 util.js
var utils=(function(){ var flg='getComputedStyle' in window; // 惰性思想的运用; function makeArray(arg){ if(flg){ // 标准浏览器 return Array.prototype.slice.call(arg); }else{ var ary=[]; for(var i=0; i<arg.length; i++){ ary.push(arg[i]); } return ary; } } function jsonParse(jsonStr){ return 'JSON' in window"htmlcode">(function () { var oTab = document.getElementById('tab'); var tHead = oTab.tHead; var tCells = tHead.rows[0].cells; var tBody = oTab.tBodies[0]; var aRows = tBody.rows; var data = null; getData(); function getData() { var xml = new XMLHttpRequest; xml.open('get', 'data.txt', false); xml.onreadystatechange = function () { if (xml.readyState === 4 && /^2\d{2}$/.test(xml.status)) { data = utils.jsonParse(xml.responseText); } }; xml.send(); } bind(); function bind() { var str = ''; for (var i = 0; i < data.length; i++) { var curData = data[i]; curData.sex = curData.sex == 0 ? '男' : '女'; str += '<tr> <td>' + curData.name + '</td> <td>' + curData.age + '</td> <td>' + curData.score + '</td> <td>' + curData.sex + '</td> </tr>'; } tBody.innerHTML = str; } changeColor(); function changeColor() { for (var i = 0; i < aRows.length; i++) { aRows[i].className = 'bg' + i % 2; } } function sort(n) { for (var i = 0; i < tCells.length; i++) { tCells[i].flag = i == n ? tCells[i].flag * -1 : -1; } var ary = utils.makeArray(aRows); ary.sort(function (a, b) { a = a.cells[n].innerHTML; b = b.cells[n].innerHTML; if (isNaN(a) && isNaN(b)) { return a.localeCompare(b) * tCells[n].flag; } return (a - b) * tCells[n].flag; }); var frg = document.createDocumentFragment(); for (var i = 0; i < ary.length; i++) { frg.appendChild(ary[i]); } tBody.appendChild(frg); frg = null; changeColor(); } for (var i = 0; i < tCells.length; i++) { if (tCells[i].className == 'cursor') { tCells[i].flag = -1; tCells[i].index = i; tCells[i].onclick = function () { sort(this.index); } } } })();以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com
暂无评论...