Javascript 动态HTML表的jQuery或JS转换

Javascript 动态HTML表的jQuery或JS转换,javascript,jquery,html-table,Javascript,Jquery,Html Table,我需要在DIV标记中找到表,并对标题进行修改。可用的报告工具不允许识别生成的表,但允许脚本和DIV插入 考虑下表,其中标题为第一行: <div id="ScrollHeader"> <table> <tr> <td>Browser</td> <td>Visits</td> <td>Pages/Visit</td> <td&g

我需要在
DIV
标记中找到
,并对标题进行修改。可用的报告工具不允许识别生成的表,但允许脚本和
DIV
插入

考虑下表,其中标题为第一行:

<div id="ScrollHeader">
  <table>
    <tr>
      <td>Browser</td>
      <td>Visits</td>
      <td>Pages/Visit</td>
      <td>Avg. Time on Site</td>
      <td>% New Visits</td>
      <td>Bounce Rate</td>
      <td>Avg. Time on Site</td>
      <td>% New Visits</td>
      <td>Bounce Rate</td>
    </tr>
    <tr>
      <td>Firefox first</td>
      <td>1,990</td>
      <td>3.11</td>
      <td>00:04:22</td>
      <td>70.00%</td>
      <td>32.61%</td>
      <td>00:04:22</td>
      <td>70.00%</td>
      <td>32.61%</td>
    </tr>
    <tr>
      <td>Firefox</td>
      <td>1,990</td>
      <td>3.11</td>
      <td>00:04:22 test test test</td>
      <td>70.00%</td>
      <td>32.61%</td>
      <td>00:04:22</td>
      <td>70.00%</td>
      <td>32.61%</td>
    </tr>
  </table>
</div>

浏览器
拜访
页面/访问
现场平均时间
%新访问
反弹率
现场平均时间
%新访问
反弹率
火狐优先
1,990
3.11
00:04:22
70.00%
32.61%
00:04:22
70.00%
32.61%
火狐
1,990
3.11
00:04:22测试
70.00%
32.61%
00:04:22
70.00%
32.61%
我需要JavaScript或jQuery代码将表转换为以下内容:

<div id="ScrollHeader">
  <table>
    <thead>
      <tr>
        <th>Browser</th>
        <th>Visits</th>
        <th>Pages/Visit</th>
        <th>Avg. Time on Site</th>
        <th>% New Visits</th>
        <th>Bounce Rate</th>
        <th>Avg. Time on Site</th>
        <th>% New Visits</th>
        <th>Bounce Rate</th>
      </tr>
    </thead>
    <tr>
      <td>Firefox first</td>
      <td class="numeric">1,990</td>
      <td class="numeric">3.11</td>
      <td class="numeric">00:04:22</td>
      <td class="numeric">70.00%</td>
      <td class="numeric">32.61%</td>
      <td class="numeric">00:04:22</td>
      <td class="numeric">70.00%</td>
      <td class="numeric">32.61%</td>
    </tr>
  </table>
</div>

浏览器
拜访
页面/访问
现场平均时间
%新访问
反弹率
现场平均时间
%新访问
反弹率
火狐优先
1,990
3.11
00:04:22
70.00%
32.61%
00:04:22
70.00%
32.61%
代码需要识别
中的表格,在第一行
TR
之前插入
THEAD
,将第一行中的
TD
更改为
TH
,并用
将其关闭

我曾尝试使用
$(“divp[id^='ScrollHeader']).length
查找
div
,并使用
$(“tr:first”)
执行
.prepend(document.createElement('thead')未成功。

尝试以下操作:

$('#ScrollHeader table tr:eq(0)').wrap('<thead />');
$('#ScrollHeader table tr:eq(0) td').each(function () {
    $('#ScrollHeader table tr:eq(0)').append($('<th />').html($(this).html()));
    $(this).remove();
});
$('ScrollHeader表tr:eq(0)')。换行('';
$('#ScrollHeader表tr:eq(0)td')。每个(函数(){
$('#ScrollHeader表tr:eq(0)').append($('').html($(this.html());
$(this.remove();
});
$(“#滚动头tr:first”)
.wrap(“”)
.children().each(函数()){
text=$(this.text();
$(this).替换为(“”+text+“”);
});
$('#ScrollHeader thead')。在('#ScrollHeader tbody')之前插入;

您可以看到它正在工作

感谢您的及时回复!它很好用。什么命令将向DIV内的表添加ID$table.find(“table”).replacetwith(“table ID='myTable'))感谢您的及时回复!它很好用。什么命令将向DIV内的表添加ID$table.find(“table”).replaceWith(“table ID='myTable')尝试以下操作:
$('#ScrollHeader table').attr('ID','myTable')
Valmir:Matías已经回答了关于为表分配ID的问题,所以我不会回答这个问题。作为一名优秀的Stackoverflow用户,您应该投票选出帮助您的答案,并接受对您的问题有最佳解决方案的答案。:)
$('#ScrollHeader tr:first')
    .wrap('<thead />')
    .children().each(function(){
        text = $(this).text();
        $(this).replaceWith('<th>'+text+'</th>');
    });
$('#ScrollHeader thead').insertBefore('#ScrollHeader tbody');