欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JavaScript合并table相同内容的单元格

程序员文章站 2024-02-28 17:40:34
...
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>JavaScript合并table相同内容的单元格</title>
</head>
<body>
    <style type="text/css">
        textarea{
            height: 400px;
        }
        table
        {
            border-collapse: collapse;
            margin: 0 auto;
            text-align: center;
        }
        table td, table th
        {
            border: 1px solid #666666;
        }
    </style>

    <script>
        / 
        //功能:合并表格 
        //参数:tb-需要合并的表格ID 
        //参数:colLength--需要对前几列进行合并,比如, 
        //想合并前两列,后面的数据列忽略合并,colLength应为2 
        //缺省表示对全部列合并
        /// 
        function mergeTableCell(tb, colLength) {
            tb = document.getElementById(tb);
            //检查表格是否规整
            if (!checkTableRegular(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) return;
                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());
                    }
                }
            }
        }

        / 
        //功能:检查表格是否规整
        //参数:tb--需要检查的table
        /
        function checkTableRegular(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>


    <table id="table1">
        <tr>
            <td>A1</td>
            <td>B1</td>
            <td>C1</td>
            <td>1</td>
        </tr>
        <tr>
            <td>A1</td>
            <td>B1</td>
            <td>C2</td>
            <td>2</td>
        </tr>
        <tr>
            <td>A1</td>
            <td>B2</td>
            <td>C3</td>
            <td>4</td>
        </tr>
        <tr>
            <td>A1</td>
            <td>B2</td>
            <td>C4</td>
            <td>4</td>
        </tr>
    </table>
    <br>
    <input type="button" value="合并相同内容的单元格" onclick="mergeTableCell('table1')">
</body>
</html>



相关标签: js