js合并表格

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;   charset=gb2312">
    <title>合并表格</title>
    <script language="javascript" type="text/javascript">
        ///////////////////////////////////////////////   
        //   功能:合并表格   
        //   参数:tb--需要合并的表格ID   
        //   参数:colLength--需要对前几列进行合并,比如,   
        //   想合并前两列,后面的数据列忽略合并,colLength应为2   
        //   缺省表示对全部列合并   
        //   data:   2005.11.6   
        ///////////////////////////////////////////////
        function uniteTable(tb, colLength) {
            //   检查表格是否规整   
            //if (!checkTable(tb)) return;
            var i = 0;
            var j = 0;
            var rowCount = tb.rows.length; //   行数   
            var colCount = tb.rows[0].cells.length; //   列数   
            var obj1 = null;
            var obj2 = null;
            //为每个单元格命名   
            for (i = 0; i < rowCount; i++) {
                for (j = 0; j < colCount; j++) {
                    tb.rows[i].cells[j].id = "tb__" + i.toString() + "_" + j.toString();
                }
            }
            //合并行   
            for (i = 0; i < colCount; i++) {
                if (i == colLength) break;
                obj1 = document.getElementById("tb__0_" + i.toString())
                for (j = 1; j < rowCount; j++) {
                    obj2 = document.getElementById("tb__" + j.toString() + "_" + i.toString());
                    if (obj1.innerText == obj2.innerText) {
                        obj1.rowSpan++;
                        obj2.parentNode.removeChild(obj2);
                    } else {
                        obj1 = document.getElementById("tb__" + j.toString() + "_" + i.toString());
                    }
                }
            }
            //合并列
            for (i = 0; i < rowCount; i++) {
                colCount = tb.rows[i].cells.length;
                obj1 = document.getElementById(tb.rows[i].cells[0].id);
                for (j = 1; j < colCount; j++) {
                    if (j >= colLength) break;
                    if (obj1.colSpan >= colLength) break;

                    obj2 = document.getElementById(tb.rows[i].cells[j].id);
                    if (obj1.innerText == obj2.innerText) {
                        obj1.colSpan++;
                        obj2.parentNode.removeChild(obj2);
                        j = j - 1;
                    }
                    else {
                        obj1 = obj2;
                        j = j + obj1.rowSpan;
                    }
                }
            }
        }

        /////////////////////////////////////////   
        //   功能:检查表格是否规整   
        //   参数:tb--需要检查的表格ID   
        //   data:   2005.11.6   
        /////////////////////////////////////////   
        function checkTable(tb) {
            if (tb.rows.length == 0) return false;
            if (tb.rows[0].cells.length == 0) return false;
            for (var i = 0; i < tb.rows.length; i++) {
                if (tb.rows[0].cells.length != tb.rows[i].cells.length) return false;
            }
            return true;
        }   
    </script>
</head>
<body>
    <table width="400" border="1" id="table1">
        <tr>
            <td>
                a
            </td>
            <td>
                for
            </td>
            <td>
                for
            </td>
            <td>
                 
            </td>
            <td>
                100
            </td>
            <td>
                200
            </td>
        </tr>
        <tr>
            <td>
                a
            </td>
            <td>
                for
            </td>
            <td>
                for1
            </td>
            <td>
                 
            </td>
            <td>
                150
            </td>
            <td>
                230
            </td>
        </tr>
        <tr>
            <td>
                a
            </td>
            <td>
                if1
            </td>
            <td>
                if1
            </td>
            <td>
                 
            </td>
            <td>
                100
            </td>
            <td>
                200
            </td>
        </tr>
        <tr>
            <td>
                a
            </td>
            <td>
                if2
            </td>
            <td>
                if2
            </td>
            <td>
                 
            </td>
            <td>
                300
            </td>
            <td>
                240
            </td>
        </tr>
        <tr>
            <td>
                a
            </td>
            <td>
                if
            </td>
            <td>
                if
            </td>
            <td>
                 
            </td>
            <td>
                320
            </td>
            <td>
                230
            </td>
        </tr>
    </table>
    <br>
    <input type="button" value="合并表格" onclick="uniteTable(table1,3)">
</body>
</html>