table排名次

时间:2021-01-24 06:34:07
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery-2.1.4.min.js" ></script>
<style>
table{border-collapse: collapse; width: 100%; text-align: center;}
table td{border: red solid 1px;}
</style>
</head>
<body>
<table class="mytb">
<tr>
<td>姓名</td>
<td>分数</td>
<td>排名</td>
</tr>
<tr >
<td>小明</td>
<td class="fs">108053</td>
<td class="pm"></td>
</tr>
<tr>
<td>小张</td>
<td class="fs">180370</td>
<td class="pm"></td>
</tr>
<tr>
<td>小丁</td>
<td class="fs">23002</td>
<td class="pm"></td>
</tr>
</table>
<p/>
<table class="mytb">
<tr>
<td>姓名</td>
<td>分数</td>
<td>排名</td>
</tr>
<tr >
<td>小明</td>
<td class="fs">50</td>
<td class="pm"></td>
</tr>
<tr>
<td>小张</td>
<td class="fs">100</td>
<td class="pm"></td>
</tr>
<tr>
<td>小丁</td>
<td class="fs">90</td>
<td class="pm"></td>
</tr>
</table> <script>
function px(table){
//把值推进数组
var arr0=[];
for(var i=1; i<$(table).find("tr").length; i++){
arr0.push($(table).find("tr").eq(i). find(".fs").text());
} //排序(此处不适合用冒泡)
function sortNumber(a, b){return b - a}
var arr=arr0.sort(sortNumber); console.log(arr);
//赋值到排名
for(var i=1; i<$(table).find("tr").length; i++){
for (var z=0; z<arr.length; z++) {
if($(table).find("tr").eq(i). find(".fs").text()==arr[z]){
$(table).find("tr").eq(i). find(".pm").text(z+1)
}
}
}
} for (var i=0; i<$(".mytb").length; i++) {
px($(".mytb").eq(i))
}
</script>
</body>
</html>