var rows = table.rows.length
var c=table.rows[0].cells[0].innerHTML
for (i=0i<rows-1i++)
{
k=i
for (j=i+1j<rowsj++)
{
if (table.rows[k].cells[0].innerHTML>table.rows[j].cells[0].innerHTML)
k=j
}
if (k>i)
{
tmp=table.rows[i].cells[0].innerHTML
table.rows[i].cells[0].innerHTML=table.rows[k].cells[0].innerHTML
table.rows[k].cells[0].innerHTML=tmp
}
}
<!DOCTYPE HTML><html>
<head>
<meta charset=UTF-8>
<title>YuGiOh</title>
<style type="text/css">
</style>
<script type="text/javascript">
window.onload = function ()
{
var table = document.getElementById ('table')
var tbody = table.tBodies[0]
var rows = tbody.rows
var cells = rows[0].cells
for ( var j = 0 j < cells.length j++)
{
cells[j].onclick = function ()
{
var array = []
for ( var i = 1 i < rows.length i++)
{
array.push (rows[i])
}
array.sort (function (a, b)
{
var rand = Math.random ()
if (rand >= 0.5)
{
return -1
}
else
{
return 1
}
})
for ( var i = 0 i < array.length i++)
{
tbody.appendChild (array[i])
}
}
}
}
</script>
</head>
<body>
<table id="table">
<tr>
<td>编号</td>
<td>姓名</td>
</tr>
<tr>
<td>1</td>
<td>李勇</td>
</tr>
<tr>
<td>2</td>
<td>王博</td>
</tr>
<tr>
<td>3</td>
<td>刘海</td>
</tr>
<tr>
<td>4</td>
<td>陈锋</td>
</tr>
</table>
</body>
</html>
<!DOCTYPE HTML><html>
<head>
<meta charset="UTF-8" />
<title>main.html</title>
<style type="text/css">
table {
width: 300pxborder : 1px solid black
border-collapse: collapse
border: 1px solid black
}
td {
border: 1px solid black
}
</style>
<script type="text/javascript">
function paiXu ()
{
var tabNode = document.getElementsByTagName ("table")[0]
var trs = tabNode.rows
var arr = new Array
for ( var x = 0x <trs.lengthx++)
{
arr.push (trs[x])
}
sortt (arr)
for ( var x = 0x <arr.lengthx++)
{
tabNode.tBodies[0].appendChild (arr[x])
}
}
function sortt (arr)
{
for ( var x = 0x <arr.lengthx++)
{
for ( var y = x + 1y <arr.lengthy++)
{
if (parseInt (arr[x].cells[1].childNodes[0].nodeValue) <= parseInt (arr[y].cells[1].childNodes[0].nodeValue))
{
var tmp = arr[x]
arr[x] = arr[y]
arr[y] = tmp
}
}
}
}
</script>
</head>
<body>
<input type="button" value="排序" onclick="paiXu()">
<table>
<tr>
<td>小明</td>
<td>23</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>小花</td>
<td>21</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>小高</td>
<td>12</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>小林</td>
<td>25</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>小王</td>
<td>35</td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</body>
</html>