정렬 테이블<br>{<br> 색상: #000000;<br> 글꼴 두께: 굵게;<br> 텍스트 장식: 없음;<br>}<br>< /SCRIPT><br></head><br><body><br><p>点击标题排序</p><br><table width="481" border="1" 셀 패딩 ="0" cellpacing="3" id="mytable" class="sortable"><br> <tbody><br> <tr><br> <th width="165">이름< /th><br> <th width="101">급여</th><br> <th width="101" align="center">연장</th><br> <th width="99" align="center">시작일</th><br> </tr><br> <tr><br> <td>블로그, Fred</td><br> <td>$12000.00</td><br> <td align="center">1353</td><br> <td align="center">2003/08/18</td> <br> </tr><br> <tr><br> <td>터비, 케빈><br> <td>$191200.00</td><br> <td = "center">2342</td><br> <td align="center">02/05/1979</td><br> </tr><br> <tr><br> <td>Mbogo, Arnold</td><br> <td>$32010.12</td><br> <td align="center">2755</td><br> <td align= "center">09/08/1998</td><br> </tr><br> <tr><br> <td>셰익스피어, 빌</td><br> <td> $122000.00</td><br> <td align="center">3211</td><br> <td align="center">12/11/1961</td><br> < ;/tr><br> <tr><br> <td>셰익스피어, 햄넷</td><br> <td>$9000</td><br> <td align="center"> ;9005</td><br> <td align="center">01/01/2002</td><br> </tr><br> <tr><br> <td> 피츠, 마빈</td><br> <td>$3300</td><br> <td align="center">5554</td><br> <td align="center"> ;22/05/1995</td><br> </tr><br> </tbody><br></table><br><p><a href="http:/ /www.flaspx.com/weblog" target="_blank">Alpha의 블로그</a></p><br></body><br></html><br><br><div class="codetitle"> <span><a style="CURSOR: pointer" data="91456" class="copybut" id="copybut91456" onclick="doCopy('code91456')"><u>复代码</u></a></span> 代码如下:</div> <div class="codebody" id="code91456"> <p><font face="Verdana">addEvent(window, "load", sortables_init);</font></p> <p><font face="Verdana">var SORT_COLUMN_INDEX;</font></p> <p><font face="Verdana">function sortables_init() {<br> // Find all tables with class sortable and make them sortable<br> if (!document.getElementsByTagName) return;<br> tbls = document.getElementsByTagName("table");<br> for (ti=0;ti<tbls.length;ti++) {<BR> thisTbl = tbls[ti];<BR> if (((' '+thisTbl.className+' ').indexOf("sortable") != -1) && (thisTbl.id)) {<BR> //initTable(thisTbl.id);<BR> ts_makeSortable(thisTbl);<BR> }<BR> }<BR>}</FONT></P> <P><FONT face=Verdana>function ts_makeSortable(table) {<BR> if (table.rows && table.rows.length > 0) {<br> var firstRow = table.rows[0];<br> }<br> if (!firstRow) return;<br><br> // We have a first row: assume it's the header, and make its contents clickable links<br> for (var i=0;i<firstRow.cells.length;i++) {<BR> var cell = firstRow.cells[i];<BR> var txt = ts_getInnerText(cell);<BR> cell.innerHTML = '<a href="#" class="sortheader" '+ <BR> 'onclick="ts_resortTable(this, '+i+');return false;">' + <br> txt+'<span class="sortarrow"></span></a>';<br> }<br>}</font></p> <p><font face="Verdana">function ts_getInnerText(el) {<br> if (typeof el == "string") return el;<br> if (typeof el == "undefined") { return el };<br> if (el.innerText) return el.innerText; //Not needed but it is faster<br> var str = "";<br><br> var cs = el.childNodes;<br> var l = cs.length;<br> for (var i = 0; i < l; i++) {<br> switch (cs[i].nodeType) {<br> case 1: //ELEMENT_NODE<br> str += ts_getInnerText(cs[i]);<br> break;<br> case 3: //TEXT_NODE<br> str += cs[i].nodeValue;<br> break;<br> }<br> }<br> return str;<br>}</font></p> <p><font face="Verdana">function ts_resortTable(lnk,clid) {<br> // get the span<br> var span;<br> for (var ci=0;ci<lnk.childNodes.length;ci++) {<br> if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci];<br> }<br> var spantext = ts_getInnerText(span);<br> var td = lnk.parentNode;<br> var column = clid || td.cellIndex;<br> var table = getParent(td,'TABLE');<br><br> // Work out a type for the column<br> if (table.rows.length <= 1) return;<br> var itm = ts_getInnerText(table.rows[1].cells[column]);<br> sortfn = ts_sort_caseinsensitive;<br> if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date;<br> if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = ts_sort_date;<br> if (itm.match(/^[$]/)) sortfn = ts_sort_currency;<br> if (itm.match(/^[\d\.]+$/)) sortfn = ts_sort_numeric;<br> SORT_COLUMN_INDEX = column;<br> var firstRow = new Array();<br> var newRows = new Array();<br> for (i=0;i<table.rows[0].length;i++) { firstRow[i] = table.rows[0][i]; }<br> for (j=1;j<table.rows.length;j++) { newRows[j-1] = table.rows[j]; }</font></p> <p><font face="Verdana"> newRows.sort(sortfn);</font></p> <p><font face="Verdana"> if (span.getAttribute("sortdir") == 'down') {<br> ARROW = '↑';<br> newRows.reverse();<br> span.setAttribute('sortdir','up');<br> } else {<br> ARROW = '↓';<br> span.setAttribute('sortdir','down');<br> }<br><br> // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones<br> // don't do sortbottom rows<br> for (i=0;i<newRows.length;i++) { if (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))) table.tBodies[0].appendChild(newRows[i]);}<br> // do sortbottom rows only<br> for (i=0;i<newRows.length;i++) { if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1)) table.tBodies[0].appendChild(newRows[i]);}<br><br> // Delete any other arrows there may be showing<br> var allspans = document.getElementsByTagName("span");<br> for (var ci=0;ci<allspans.length;ci++) {<br> if (allspans[ci].className == 'sortarrow') {<br> if (getParent(allspans[ci],"table") == getParent(lnk,"table")) { // in the same table as us?<br> allspans[ci].innerHTML = '';<br> }<br> }<br> }<br><br> span.innerHTML = ARROW;<br>}</font></p> <p><font face="Verdana">function getParent(el, pTagName) {<br> if (el == null) return null;<br> else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) // Gecko bug, supposed to be uppercase<br> return el;<br> else<br> return getParent(el.parentNode, pTagName);<br>}<br>function ts_sort_date(a,b) {<br> // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX<br> aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);<br> bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);<br> if (aa.length == 10) {<br> dt1 = aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2);<br> } else {<br> yr = aa.substr(6,2);<br> if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }<br> dt1 = yr+aa.substr(3,2)+aa.substr(0,2);<br> }<br> if (bb.length == 10) {<br> dt2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2);<br> } else {<br> yr = bb.substr(6,2);<br> if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }<br> dt2 = yr+bb.substr(3,2)+bb.substr(0,2);<br> }<br> if (dt1==dt2) return 0;<br> if (dt1<dt2) return -1;<br> return 1;<br>}</font></p> <p><font face="Verdana">function ts_sort_currency(a,b) { <br> aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');<br> bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');<br> return parseFloat(aa) - parseFloat(bb);<br>}</font></p> <p><font face="Verdana">function ts_sort_numeric(a,b) { <br> aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]));<br> if (isNaN(aa)) aa = 0;<br> bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX])); <br> if (isNaN(bb)) bb = 0;<br> return aa-bb;<br>}</font></p> <p><font face="Verdana">function ts_sort_caseinsensitive(a,b) {<br> aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase();<br> bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase();<br> if (aa==bb) return 0;<br> if (aa<bb) return -1;<br> return 1;<br>}</font></p> <p><font face="Verdana">function ts_sort_default(a,b) {<br> aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);<br> bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);<br> if (aa ==bb) return 0;<br> if (aa<bb) return -1;<br> return 1;<br>}</font></p> <p><font face="Verdana"><br>함수 addEvent(elm, evType, fn, useCapture)<br>// addEvent 및 RemoveEvent<br>// IE5, NS6 및 Mozilla에 대한 크로스 브라우저 이벤트 처리<br>// By 스콧 앤드류<br>{<br> if (elm.addEventListener){<br> elm.addEventListener(evType, fn, useCapture);<br> return true;<br> } else if (elm.attachEvent){<br> var r = elm.attachEvent("on" evType, fn);<br> return r;<br> } else {<br> Alert("핸들러를 제거할 수 없습니다.");<br> }<br>} <br></font></p> </div></font>