在IE的table,tbody,thead, tfoot中,它們都支援一個叫moveRow(indexToMove, destinationIndex)的API,第一個參數要移去的行號,第二個參數為當前的行號,行號即為rowIndex。有了,我們就可以簡捷地交換兩行位置,而不需要冗長的insertBefore(它還要根據是上移下是下移,修改參數呢)。
<br /><!doctype html><br /><html><br /> <head><br /> <title>交換行的位置 by 司徒正美</title><br /> <script><br /> window.onload = function(){<br /> var tb = document.getElementById("table");<br /> tb.moveRow(2,1)<br /> }<br /> </script><br /> <style><br /> .table{<br /> width:60%;<br /> border: 1px solid red;<br /> border-collapse: collapse;<br /> }<br /> .table td{<br /> border: 1px solid red;<br /> height: 20px;<br /> }<br /> </style><br /> </head><br /> <body><br /> <h1>行交換 by 司徒正美</h1><br /> <table class="table"><br /> <tbody id="table"><br /> <tr><br /> <td>0</td><br /> <td>One</td><br /> <td>dom.require</td><br /> </tr><br /> <tr id="2" ><br /> <td class="2">1</td><br /> <td>Two</td><br /> <td>ControlJS </td><br /> </tr><br /> <tr id="3" ><br /> <td class="3">2</td><br /> <td>Three</td><br /> <td>HeadJS</td><br /> </tr><br /> <tr id="4" ><br /> <td class="4">3</td><br /> <td>Four</td><br /> <td>LAB.js</td><br /> </tr><br /> <tr id="5" ><br /> <td class="5">4</td><br /> <td>Five</td><br /> <td>$script.js</td><br /> </tr><br /> <tr id="6" ><br /> <td class="6">5</td><br /> <td>Six</td><br /> <td>NBL.js</td><br /> </tr><br /> </tbody><br /> </table><br /> </body><br /></html><br />
運行代碼