html中table数据排序的js代码
对了,注意那个innerText和innerHTML
代码如下:
function sortCells(event) { var obj = event.target; var count = 0; count是记录点击次数的,根据奇偶进行升序或降序 if(!obj.getAttribute("clickCount")){ obj.setAttribute("clickCount", 0); } else { count = parseInt(obj.getAttribute("clickCount")); count++; obj.setAttribute("clickCount", count); } var table = event.target.parentNode.parentNode; if(table.nodeName.localeCompare("THEAD") == 0){ if(table.parentNode.nodeName.localeCompare("TABLE") == 0){ table = table.parentNode; } else { return; } } else if(table.nodeName.localeCompare("TBODY") == 0){ if(table.parentNode.nodeName.localeCompare("TABLE") == 0){ table = table.parentNode; } else { return; } } else if(table.nodeName.localeCompare("TABLE") == 0){ } else { return; } var colNum; for(x = 0; x < table.rows(1).cells.length; x++){ if(event.target.innerText.localeCompare(table.rows(0).cells[x].innerText) == 0){ colNum = x; break; } } var column = table.rows(1).cells.length; var row = table.rows.length; var Ar = new Array(row - 1); for (x = 0; x < row - 1; x++) { Ar[x] = new Array(column); } for (x = 1; x < row; x++) { for (y = 0; y < column; y++) { Ar[x - 1][y] = table.rows(x).cells(y).innerHTML; } } //这个可以对字符串进行本地化排序 /* if((count%2) == 0){ Ar.sort(function(a, b) { return b[colNum].localeCompare(a[colNum]) }); } else { Ar.sort(function(a, b) { return a[colNum].localeCompare(b[colNum]) }); } */ var temp; for (x = 0; x < row - 1; x++) { for (y = 1; y < row - 1; y++) { temp = Ar[y - 1]; if((count % 2) == 0){ if (parseInt(Ar[y - 1][colNum]) >= parseInt(Ar[y][colNum])) { Ar[y - 1] = Ar[y]; Ar[y] = temp; } } else { if (parseInt(Ar[y - 1][colNum]) <= parseInt(Ar[y][colNum])) { Ar[y - 1] = Ar[y]; Ar[y] = temp; } } } } for (x = 1; x < row; x++) { for (y = 0; y < column; y++) { table.rows(x).cells(y).innerHTML = Ar[x - 1][y]; } } count++; }
相关推荐
tianqi 2020-07-05
lyg0 2020-05-28
世樹 2020-11-11
SCNUHB 2020-11-10
bleach00 2020-11-10
FellowYourHeart 2020-10-05
momode 2020-09-11
思君夜未眠 2020-09-04
jessieHJ 2020-08-19
行吟阁 2020-08-09
表格的现在还是较为常用的一种标签,但不是用来布局,常见处理、显示表格式数据。在HTML网页中,要想创建表格,就需要使用表格相关的标签。<table> <tr> <td>单元格内的文字</td> ...
gufudhn 2020-08-09
末点 2020-08-03
nimeijian 2020-07-30
好记忆也需烂 2020-07-28
zlsdmx 2020-07-05
tomson 2020-07-05
onlykg 2020-07-04