簡體   English   中英

使用JavaScript將HTML表格的第一行移到thead標簽下

[英]Move first row of a HTML table under a thead tag using JavaScript

我有一個BIRT生成的html表,如下所示:

<table id="myTableID">
    <tr>
        <th></th>
        <th></th>
        <th></th>
    </tr>

    <tr>
        <th></th>
        <th></th>
        <th></th>
    </tr>

    <tr>
        <th></th>
        <th></th>
        <th></th>
    </tr>
</table>

我想編寫一個JavaScript代碼來讀取此表,並以以下形式對其進行重寫:在<thead>標記中獲取表的第一行,在<tbody>標記中獲取表的其余行:

<table id="myTableID">
    <thead>
        <tr>
            <th></th>
            <th></th>
            <th></th> 
        </tr>
    </thead>

    <tbody>
        <tr>
            <th></th>
            <th></th>
            <th></th>
        </tr>

        <tr>
            <th></th>
            <th></th>
            <th></th>
        </tr>
    </tbody>    
</table>

我對JavaScript有基本的了解,但是不知道如何進行這種情況。 請任何幫助?

  1. 使用prependTo()插入thead元素
  2. 使用append()將第一個tr插入thead

 $('<thead></thead>').prependTo('#myTableID').append($('#myTableID tr:first')); console.log($('#myTableID')[0].outerHTML); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <table id="myTableID"> <tr> <th></th> <th></th> <th></th> </tr> <tr> <th></th> <th></th> <th></th> </tr> <tr> <th></th> <th></th> <th></th> </tr> </table> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM