1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
<!DOCTYPE HTML> <html lang= "zh-cn" >
<head> <meta charset= "utf-8"
/>
<meta http-equiv= "Content-Language"
content= "zh-cn"
/>
<title>对商品按价格进行排序</title> <meta name= "keywords"
content= ""
/>
<meta name= "description"
content= ""
/>
<style type= "text/css" >
*{ margin: 0px; padding: 0px; font-size:14px; font-family: ‘微软雅黑‘ ; font-size:12px;
} .clearfix:after{content: ‘.‘ ;display:block;height:0;clear:both;visibility:hidden}
.clearfix{display:inline-block;} * html .clearfix{height:1%} .clearfix{display:black;} ul li{ list-style:none;
} </style> </head> <body> <script type= "text/javascript" >
window.onload = function(){ var
oBtnT = document.getElementById( ‘btn1‘ );
var
oBtnB = document.getElementById( ‘btn2‘ );
var
oTab = document.getElementById( ‘tab1‘ );
var
arr = [];
var
oTr = oTab.tBodies[0].rows;
oBtnT.onclick = function(){
tab();
for ( var
i=0;i<arr.length;i++){
//alert(arr[i].cells[1].innerHTML);
oTab.tBodies[0].appendChild(arr[i]);
}
};
oBtnB.onclick = function(){
tab();
for ( var
i=0;i<arr.length;i++){
//alert(arr[i].cells[1].innerHTML);
oTab.tBodies[0].insertBefore(arr[i],oTr[0]);
}
};
function tab(){
for ( var
i=0;i<oTr.length;i++){
arr[i] = oTr[i];
}
arr.sort(function(t1,t2){
var
n1 = parseInt(t1.cells[3].innerHTML);
var
n2 = parseInt(t2.cells[3].innerHTML);
return
n1-n2;
});
};
} </script> <input type= ‘button‘
id= ‘btn1‘
value= ‘价格↑‘ >
<input type= ‘button‘
id= ‘btn2‘
value= ‘价格↓‘ >
<table id= ‘tab1‘
width= ‘400‘ >
<thead>
<td>ID</td>
<td>姓名</td>
<td>年龄</td>
<td>价格</td>
</thead>
<tbody>
<tr>
<td>1</td>
<td>黑哥</td>
<td>22</td>
<td>5</td>
</tr>
<tr>
<td>2</td>
<td>宇哥</td>
<td>24</td>
<td>8</td>
</tr>
<tr>
<td>3</td>
<td>荡哥</td>
<td>26</td>
<td>11</td>
</tr>
<tr>
<td>4</td>
<td>二哥</td>
<td>22</td>
<td>9</td>
</tr>
<tr>
<td>5</td>
<td>段少</td>
<td>22</td>
<td>2</td>
</tr>
<tr>
<td>6</td>
<td>老群主</td>
<td>22</td>
<td>30</td>
</tr>
<tr>
<td>6</td>
<td>牛儿</td>
<td>22</td>
<td>31</td>
</tr>
</tbody>
</table> </body> </html> |
js 对商品价格进行排序(对于小数后面有待优化)