Front-end pen exam notes: enables sorting of table headers in ascending/descending order in HTML
Question: for example, please sort table information. When you click the attribute area of the header, you can switch the order of table information. That is, the first click is sort in descending order, click again to sort in ascending order.
Name |
Power |
Agility |
Intelligence |
Druid |
17 |
24 |
13 |
Monthly knight |
15 |
22 |
16 |
King of the Gods |
19 |
15 |
20 |
Stray swordsman |
23 |
15 |
14 |
Basic Ideas: Created with Rapha? L 2.1.2 Click Store the values of each column into an array First click? (ClassName = ""?) Sort in ascending order (className = "desc ") Assign new values to each column in the order of newly arranged Arrays Sort in descending order (className = "") Yes No Complete code:
Name |
Power |
Agility |
Intelligence |
Druid |
17 |
24 |
13 |
Monthly knight |
15 |
22 |
16 |
King of the Gods |
19 |
15 |
20 |
Stray swordsman |
23 |
15 |
14 |
<script type="text/javascript"> var tag=1; function sortNumberAS(a, b) { return a - b } function sortNumberDesc(a, b) { return b-a } function SortTable(obj){ var td0s=document.getElementsByName("td0"); var td1s=document.getElementsByName("td1"); var td2s=document.getElementsByName("td2"); var td3s=document.getElementsByName("td3"); var tdArray0=[]; var tdArray1=[]; var tdArray2=[]; var tdArray3=[]; for(var i=0;i