简体   繁体   English

使用JavaScript将HTML表格的第一行移到thead标签下

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

I have a html table generated by BIRT as follow: 我有一个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>

I want to write a JavaScript code that reads this table and re-writes it in the following form: getting the first row of the table in a <thead> tag, and the rest of the table in the <tbody> tag: 我想编写一个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>

I have a basic knowledge about the JavaScript, but I have no clue how to proceed with such case. 我对JavaScript有基本的了解,但是不知道如何进行这种情况。 Please any help? 请任何帮助?

  1. Use prependTo() to insert the thead element 使用prependTo()插入thead元素
  2. Use append() to insert the first tr inside the thead 使用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