table排序
function sort() {
var items = document.getElementById("items");
for(var i=1,length=items.cells.length;i<length;i++){
items.cells[i].index = i;
items.cells[i].onclick = function(){
console.time("hello");
var function_id = this.getAttribute("id");
if (function_id.indexOf("sort_") !== -1) {
function_id = function_id.substr(5);
};
var elements = window.table.getElementsByClassName(function_id,"td");
var oTR = [];
for (var i=0,length=elements.length;i<length;i++) {
var tmp_td = {};
tmp_td.dom = elements[i];
tmp_td.num = elements[i].innerHTML || 0;
oTR[oTR.length] = tmp_td;
};
if(this.index == window.sort_index){
//表示已经排序过了,只需要将其反序排列即可
oTR.reverse();
}else{
oTR.sort(function(a, b) {
return a.num - b.num
});
}
var fragment = document.createDocumentFragment();
for (var i=0;i<length;i++) {
fragment.appendChild(oTR[i].dom.parentNode);
};
window.sort_index = this.index;
window.table.appendChild(fragment);
console.timeEnd("hello");
}
}
}