1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script type="text/javascript"> window.onload = function(){ var tab = document.getElementById('tab'); var btn = document.getElementById('btn'); var search = document.getElementById('search'); var ipt = document.getElementById('ipt'); var sort = document.getElementById('sort'); var oldColor = ''; var nows = tab.tBodies[0].rows.length + 1; var sortSignal = true; setColor(); function setColor() { for (var i = 0; i < tab.tBodies[0].rows.length; i++) { tab.tBodies[0].rows[i].style.background = i % 2 ? '#ccc' : ''; tab.tBodies[0].rows[i].onmouseover = function() { oldColor = this.style.backgroundColor; this.style.background = 'yellow'; } tab.tBodies[0].rows[i].onmouseout = function() { this.style.background = oldColor; } } } search.onclick = function() { var txt = ipt.value.toLowerCase(); var signal = false; var noNull = true; if(txt === '') { alert('请输入文字再查找'); return; } for (var i = 0; i < tab.tBodies[0].rows.length; i++) { var target = tab.tBodies[0].rows[i].cells[1].innerHTML.toLowerCase(); var arr = txt.split(' '); oldColor = tab.tBodies[0].rows[i].style.backgroundColor; for (var j = 0; j < arr.length; j++) { signal = target.search(arr[j]) !== -1; break; } if(signal){ tab.tBodies[0].rows[i].style.background = 'yellow'; }else{ tab.tBodies[0].rows[i].style.background = oldColor; } } ipt.value = ''; } btn.onclick = function() { var oTr = document.createElement('tr'); var oTd = null; var oTd = document.createElement('td'); oTd.innerHTML = nows++; oTr.appendChild(oTd);
oTd = document.createElement('td'); oTd.innerHTML = ipt.value; oTr.appendChild(oTd);
oTd = document.createElement('td'); oTd.innerHTML = '<a href='javascript:;'>删除</a>'; oTr.appendChild(oTd); oTd.getElementsByTagName('a')[0].onclick = function() { tab.tBodies[0].removeChild(this.parentNode.parentNode); } tab.tBodies[0].appendChild(oTr); ipt.value = ''; setColor(); } sort.onclick = function() { var arr = []; for(var i = 0; i < tab.tBodies[0].rows.length; i++) { arr[i] = tab.tBodies[0].rows[i]; } arr.sort(function(arr1,arr2) { if(sortSignal){ return parseInt(arr1.cells[0].innerHTML) - parseInt(arr2.cells[0].innerHTML); }else{ return parseInt(arr2.cells[0].innerHTML) - parseInt(arr1.cells[0].innerHTML); } }); for (var i = 0; i < arr.length; i++) { tab.tBodies[0].appendChild(arr[i]); setColor(); } if (sortSignal) { sortSignal = false; sort.value = '降序'; }else{ sortSignal = true; sort.value = '升序'; } } } </script> </head> <body> <input type="text" id="ipt"/> <input type="button" id="btn" value="添加"/> <input type="button" id="search" value="搜索"/> <input type="button" id="sort" value="升序"/> <table border="1" width="600" id="tab"> <thead> <td>ID</td> <td>姓名</td> <td>操作</td> </thead> <tbody> <tr> <td>3</td> <td>刘彪</td> <td></td> </tr> <tr> <td>1</td> <td>刘鱼阳</td> <td></td> </tr> <tr> <td>2</td> <td>刘铭俊</td> <td></td> </tr> <tr> <td>5</td> <td>XXX</td> <td></td> </tr> <tr> <td>4</td> <td>二狗</td> <td></td> </tr> <tr> <td>7</td> <td>八戒</td> <td></td> </tr> <tr> <td>6</td> <td>老王</td> <td></td> </tr> <tr> <td>8</td> <td>悟空</td> <td></td> </tr> </tbody> </table> </body> </html>
|