📜  使用 JavaScript 隐藏表格列 - Html 代码示例

📅  最后修改于: 2022-03-11 14:53:03.387000             🧑  作者: Mango

代码示例1


    Hide table column using JavaScript
    
        function hideCol() {
            var col = document.getElementById("txtCol").value;
            if (isNaN(col) || col == "") {
                alert("Invalid Column");
                return;
            }
            col = parseInt(col, 10);
            col = col - 1;
            var tbl = document.getElementById("tblMain");
            if (tbl != null) {
                if (col < 0 || col >= tbl.rows.length - 1) {
                    alert("Invalid Column");
                    return;
                }
                for (var i = 0; i < tbl.rows.length; i++) {
                    for (var j = 0; j < tbl.rows[i].cells.length; j++) {
                        tbl.rows[i].cells[j].style.display = "";
                        if (j == col)
                            tbl.rows[i].cells[j].style.display = "none";
                    }
                }
            }
        }
    


    
        Enter Column Number :                           (Enter column number in the Textbox and click on Hide Example:1,2,3,..)                       
                                           Heading 1                                           Heading 2                                           Heading 3                                           Heading 4                                                             R1C1                                           R1C2                                           R1C3                                           R1C4                                                             R2C1                                           R2C2                                           R2C3                                           R2C4                                                             R3C1                                           R3C2                                           R3C3                                           R3C4                                                             R4C1                                           R4C2                                           R4C3                                           R4C4