<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<title>jstable点击表头排序</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
var dom = (document.getElementsByTagName) ? true : false;
var ie5 = (document.getElementsByTagName && document.all) ? true : false;
function initSortTable() {
arrowUp = document.createElement("SPAN");
var tn = document.createTextNode("5");
arrowUp.className = "arrow";
arrowDown = document.createElement("SPAN");
var tn = document.createTextNode("6");
arrowDown.appendChild(tn);
arrowDown.className = "arrow";
function sortTable(tableNode, nCol, bDesc, sType) {
var tBody = tableNode.tBodies[0];
for (var i = 0; i < trl; i++) {
window.status = "Sorting data...";
a.sort(compareByColumn(nCol,bDesc,sType));
window.status = "Sorting data done";
for (var i = 0; i < trl; i++) {
window.status = "Updating row " + (i + 1) + " of " + trl +
" (Time spent: " + (new Date - start) + "ms)";
if (typeof tableNode.onsort == "string")
tableNode.onsort = new Function("", tableNode.onsort);
if (typeof tableNode.onsort == "function")
function CaseInsensitiveString(s) {
return String(s).toUpperCase();
return Date.parse(s.replace(/\/-/g, '/'));
return Number(s.replace(/[^0-9/.]/g, ""));
function compareByColumn(nCol, bDescending, sType) {
else if (sType == "Date")
else if (sType == "CaseInsensitiveString")
fTypeCast = CaseInsensitiveString;
return function (n1, n2) {
if (fTypeCast(getInnerText(n1.cells[c])) < fTypeCast(getInnerText(n2.cells[c])))
if (fTypeCast(getInnerText(n1.cells[c])) > fTypeCast(getInnerText(n2.cells[c])))
function sortColumnWithHold(e) {
var el = ie5 ? e.srcElement : e.target;
var table = getParent(el, "TABLE");
var oldCursor = table.style.cursor;
var oldClick = table.onclick;
table.style.cursor = "wait";
var fakeEvent = {srcElement : e.srcElement, target : e.target};
window.setTimeout(function () {
table.style.cursor = oldCursor;
table.onclick = oldClick;
var tmp = e.target ? e.target : e.srcElement;
var tHeadParent = getParent(tmp, "THEAD");
var el = getParent(tmp, "TD");
el._descending = !Boolean(el._descending);
if (tHeadParent.arrow != null) {
if (tHeadParent.arrow.parentNode != el) {
tHeadParent.arrow.parentNode._descending = null;
tHeadParent.arrow.parentNode.removeChild(tHeadParent.arrow);
tHeadParent.arrow = arrowUp.cloneNode(true);
tHeadParent.arrow = arrowDown.cloneNode(true);
el.appendChild(tHeadParent.arrow);
for (i = 0; i < l; i++) {
if (cells[i] == el) break;
var table = getParent(el, "TABLE");
sortTable(table,i,el._descending, el.getAttribute("type"));
function getInnerText(el) {
if (ie5) return el.innerText;
for (var i = 0; i < l; i++) {
switch (cs[i].nodeType) {
str += getInnerText(cs[i]);
function getParent(el, pTagName) {
if (el == null) return null;
else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())
return getParent(el.parentNode, pTagName);
td {color: windowtext; font: menu; padding: 1px; padding-left: 5px; padding-right: 5px;
border-right: 1px solid buttonshadow;
border-bottom: 1px solid buttonshadow;
table {border-top: 1px solid buttonshadow;
border-left: 1px solid buttonshadow;
border-right: 1px solid buttonhighlight;
border-bottom: 1px solid buttonhighlight;
thead td {background: buttonface; font: menu; border: 1px outset white;
padding-top: 0; padding: bottom: 0;
border-top: 1px solid buttonhighlight;
border-left: 1px solid buttonhighlight;
border-right: 1px solid buttonshadow;
border-bottom: 1px solid buttonshadow;
thead .arrow{font-family: webdings; color: black; padding: 0; font-size: 10px;
height: 11px; width: 10px; overflow: hidden;
margin-bottom: 5; margin-top: -3; padding: 0; padding-top: 0; padding-bottom: 2;}
<script type="text/javascript" src="tablesort.js"></script>
<link type="text/css" rel="StyleSheet" href="tablesort.css" rel="external nofollow" />
font-family: Verdana, Helvetica, Arial, Sans-Serif;
<table border="1" cellpadding="10" width="100%" cellspacing="0" cellpadding="1" id="MyHeadTab" cellspacing="0" onclick="sortColumn(event)">
<td title="CaseInsensitiveString">String</td>