2014-05-13 25 views
0

是否有可能使用javascript將第一行移動到<thead>標記中?Javascript把第一行放在thead

<table id="TBL1399802283490" class="confluenceTable"> 
    <tbody> 
    <tr> 
     <th class="confluenceTh" style="cursor: pointer;">Server Name </th> 
     <th class="confluenceTh" title="null" style="cursor: pointer;">Network Zone </th> 
     <th class="confluenceTh" title="null" style="cursor: pointer;">Operational Status </th> 
    </tr> 
    <tr> 
     <td class="confluenceTd"><div style="left:1em;right:1em"> w264521f </div> </td> 
     <td class="confluenceTd"><div style="left:1em;right:1em"> GREEN </div> </td> 
     <td class="confluenceTd"><div style="left:1em;right:1em"> READY </div> </td>   
    </tr> 
    </tbody> 
</table> 

,這已經成爲

<table id="TBL1399802283490" class="confluenceTable"> 
    <thead> 
     <tr> 
     <th class="confluenceTh" style="cursor: pointer;">Server Name </th> 
     <th class="confluenceTh" title="null" style="cursor: pointer;">Network Zone </th> 
     <th class="confluenceTh" title="null" style="cursor: pointer;">Operational Status </th> 
    </tr> 
    </thead> 
    <tbody>   
    <tr> 
     <td class="confluenceTd"><div style="left:1em;right:1em"> w264521f </div> </td> 
     <td class="confluenceTd"><div style="left:1em;right:1em"> GREEN </div> </td> 
     <td class="confluenceTd"><div style="left:1em;right:1em"> READY </div> </td>   
    </tr> 
    </tbody> 
</table> 

我不是太熟悉JavaScript,因此任何幫助深表感謝!

+0

您如何生成表格? – user3470953

+0

使用clone()將內容從一個地方複製到另一個地方。 –

回答

0

這應該適用於您的代碼中給出的表格,對於通用表格,最好以更安全的方式獲取元素。

var table = document.getElementById('TBL1399802283490'); 
var thead = document.createElement('THEAD'); 
var tbody = table.getElementsByTagName('TBODY')[0]; 
var tr = table.getElementsByTagName('TR')[0]; 

table.appendChild(thead); 
tbody.removeChild(tr); 
thead.appendChild(tr);