javascript怎么实现动态在某个<tr></tr>标签添加父标签。<table id="tab1" border="1">
<tr>
<td>A</td>
<td>B</td>
</tr> <tr>
<td>A1</td>
<td>B1</td>
</tr> <tr>
<td>A2</td>
<td>B2</td>
</tr>
<tr>
<td>A3</td>
<td>B3</td>
</tr></table>怎么在第N个<tr></tr>添加父标签<thead>如想在A,B的<tr></tr>上添加成
<thead>
  <tr>
     <td>A</td>
     <td>A</td>
  </tr>
</thead>
或者在 A3,A4上添加成 
<tfood>
  <tr>
     <td>A3</td>
     <td>A4</td>
  </tr>
</tfood>

解决方案 »

  1.   

    搞半天才发现你这个需求实现不了,浏览器会自动创建tbody对象,并把所有的行添加到tbody对象中。导致在tbody对象中添加thead或tfoot对象,这个操作是非法的。
      

  2.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script type="text/javascript">
    function wrap(id, tag, index, wrapperTag) {
    var obj = document.getElementById(id);
    var targetObj = obj.getElementsByTagName(tag)[index]; //待操作的目标对象
    var cloneObj = targetObj.cloneNode(true); //克隆一遍
    var wrapper = document.createElement(wrapperTag); //创建指定的父标签
    wrapper.appendChild(cloneObj); //把克隆得来的对象插入wrapper节点
    obj.insertBefore(wrapper, targetObj); //把wrapper节点添加到目标对象前面
    obj.removeChild(targetObj); //删除目标节点
    }window.onload = function() {
    wrap('demo', 'p', 1, 'div');  //成功
    wrap('tab1', 'tr', 0, 'thead');  //失败
    }
    </script>
    </head><body>
        <table id="tab1" border="1">
            <tr>
                <td>A</td>
                <td>B</td>
            </tr>
            <tr>
                <td>A1</td>
                <td>B1</td>
            </tr>
            <tr>
                <td>A2</td>
                <td>B2</td>
            </tr>
            <tr>
                <td>A3</td>
                <td>B3</td>
            </tr>
        </table>
        
        <div id="demo">
         <p>第一行</p>
            <p>第二行</p>
            <p>第三行</p>
        </div>
    </body>
    </html>
      

  3.   

    thead、tfoot要么不用,用了就必须同时使用thead、tbody、tfoot标签对表格行进行分组。如果你只是想把表格的最前面的一行或几行套上thead标签,同时把表格最后的一行或几行套上tfoot标签,这是可以做到的。
      

  4.   

        我就只是想前N行套上thead标签而已。
      

  5.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <style type="text/css">
    thead { color:red; }
    </style>
    <script type="text/javascript">
    window.onload = function() {
    //前2行套上thead标签
    var table = document.getElementById('tab1');
    var row_01 = table.getElementsByTagName('tr')[0];
    var row_02 = table.getElementsByTagName('tr')[1];
    var cloneRow_01 = row_01.cloneNode(true);
    var cloneRow_02 = row_02.cloneNode(true);
    var thead = document.createElement('thead');
    var tbody = table.getElementsByTagName('tbody')[0];
    thead.appendChild(cloneRow_01);
    thead.appendChild(cloneRow_02);
    tbody.removeChild(row_01);
    tbody.removeChild(row_02);
    table.insertBefore(thead, tbody);
    }
    </script>
    </head><body>
        <table id="tab1" border="1">
            <tr>
                <td>A</td>
                <td>B</td>
            </tr>
            <tr>
                <td>A1</td>
                <td>B1</td>
            </tr>
            <tr>
                <td>A2</td>
                <td>B2</td>
            </tr>
            <tr>
                <td>A3</td>
                <td>B3</td>
            </tr>
        </table>
    </body>
    </html>
      

  6.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title><script type="text/javascript">
    window.onload = function() {
        //前2行套上thead标签
        var table = document.getElementById('tab1');    var row_01 = table.getElementsByTagName('tr')[0];
        var row_02 = table.getElementsByTagName('tr')[1];
    var row_03 = table.getElementsByTagName('tr')[3];    var cloneRow_01 = row_01.cloneNode(true);
        var cloneRow_02 = row_02.cloneNode(true);
    var cloneRow_03 = row_02.cloneNode(true);    var thead = document.createElement('thead');
        var tbody = table.getElementsByTagName('tbody')[0];
        thead.appendChild(cloneRow_01);
        thead.appendChild(cloneRow_02);
        tbody.insertBefore(thead,row_01);
    tbody.removeChild(row_01);
        tbody.removeChild(row_02); alert(table.outerHTML);
    }
    </script>
    </head><body>
        <table id="tab1" border="1">
            <tr>
                <td>A</td>
                <td>B</td>
            </tr>
            <tr>
                <td>A1</td>
                <td>B1</td>
            </tr>
            <tr>
                <td>A2</td>
                <td>B2</td>
            </tr>
            <tr>
                <td>A3</td>
                <td>B3</td>
            </tr>
        </table>
    </body>
    </html>
    安照你的方法,确实可以添加到THEAD \TFOOD 
    但页面表格的显示却变了,为什么会这样?