jquery 实现表格列折叠

效果图

 

<!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>
    <title></title>
    <script language="javascript" src="../Scripts/jquery-1.4.1.js"></script>
    <script language="javascript">
        $(document).ready(function () {
            //图片链接
            $("img").click(function () {
                alert(this.id);
            });
            //隐藏内容列,插入空列用于占位
            $(":radio[tag='0']").click(function () {
                var colIndex = 0;

                var th = $(this).parent();
                var tr = $(th).parent();
                var table = $(tr).parent();
                var ths = $(tr).find("th");
                //alert(ths[3]);
                var row = $(table).find("tr").index(tr);
                var col = $(tr).find("th").index(th);
                var colspan = $(th).attr("colspan");

                for (var i = 0; i < col; i++) {
                    var temp = $(ths[i]).attr("colspan");
                    colIndex += temp;
                }
                //alert("index=" + colIndex);
                //alert("row=" + row + " col=" + col + " colspan=" + colspan);

                for (var i = colIndex; i < colIndex + colspan; i++) {
                    $("td:eq(" + i + ")", $("tr:gt(1)")).hide();
                    $("td:eq(" + (i-2) + ")", $("tr:eq(1)")).hide();
                }
                $(th).hide();


                $(th).before("<th> <input type=\"radio\" tag='1'/></th>");
                $("td:eq(" + colIndex + ")", $("tr:gt(1)")).before("<td></td>");
                $("td:eq(" + (colIndex-2) + ")", $("tr:eq(1)")).before("<td></td>");
            });
            //显示内容列,删除空列
            $(":radio[tag='1']").live("click", function () {
                var colIndex = 0;

                var th = $(this).parent();
                var tr = $(th).parent();
                var table = $(tr).parent();
                var ths = $(tr).find("th");

                var row = $(table).find("tr").index(tr);
                var col = $(tr).find("th").index(th);

                var taskth = $(ths[col + 1]);
                var colspan = $(taskth).attr("colspan");

                for (var i = 0; i < col; i++) {
                    var temp = $(ths[i]).attr("colspan");
                    colIndex += temp;
                }

                for (var i = colIndex + 1; i < colIndex + colspan + 1; i++) {
                    $("td:eq(" + i + ")", $("tr:gt(1)")).show();
                    $("td:eq(" + (i - 2) + ")", $("tr:eq(1)")).show();
                }
                $(taskth).show();

                $(th).remove();
                $("td:eq(" + colIndex + ")", $("tr:gt(1)")).remove();
                $("td:eq(" + (colIndex - 2) + ")", $("tr:eq(1)")).remove();
            });

        });

    </script>

    <style type="text/css">
        table
        {
            border-collapse: collapse;
            border: none;
            table-layout:fixed;
        }
        td
        {
          text-align:center;
          width: 30px;
          overflow:hidden;
          white-space:nowrap;
          border: solid #000 1px;
        }
        th
        {
            width : 30px;
            overflow:hidden;
            white-space:nowrap;
            border: solid #000 1px;
        }
        img
        {
            CURSOR: hand;    
        }
    </style>
</head>
<body>

    <table>
        <tr>
            <th align="center" class="style1" rowspan="2">
                名称</th>
            <th align="center" class="style2" rowspan="2">
                得分</th>

            <th align="center" class="style3" colspan="2">
                <input id="Radio1" name="R1" type="radio" value="V1" tag="0"/><br />
                任务1</th>

            <th align="center" class="style4" colspan="2">
                <input id="Radio2" name="R2" type="radio" value="V1" tag="0"/><br />
                任务2</th>
            <th align="center" class="style4" colspan="3">
                <input id="Radio3" name="R2" type="radio" value="V1" tag="0"/><br />
                任务3</th>
        </tr>
        <tr>
            <td>
                列列列1</td>
            <td>
                列列列2</td>

            <td>
                列列列3</td>
            <td>
                列列列4</td>

            <td>列列列5</td>
            <td>列列列6</td>
            <td>列列列7</td>
            
        </tr>
        <tr>
            <td>
                ne1</td>
            <td >
                99</td>

            <td>
                <img id="1" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                <img id="2" alt="" src="../images/blue_button_bg.gif" /></td>

            <td>
                <img id="3" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                 <img id="4" alt="" src="../images/blue_button_bg.gif" /></td>

            <td><img id="Img1" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img2" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img3" alt="" src="../images/blue_button_bg.gif" /></td>
        </tr>

        <tr>
            <td>
                ne1</td>
            <td >
                99</td>

            <td>
                <img id="Img4" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                <img id="Img5" alt="" src="../images/blue_button_bg.gif" /></td>

            <td>
                <img id="Img6" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                 <img id="Img7" alt="" src="../images/blue_button_bg.gif" /></td>

            <td><img id="Img8" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img9" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img10" alt="" src="../images/blue_button_bg.gif" /></td>
        </tr>

        <tr>
            <td>
                ne1</td>
            <td >
                99</td>

            <td>
                <img id="Img11" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                <img id="Img12" alt="" src="../images/blue_button_bg.gif" /></td>

            <td>
                <img id="Img13" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                 <img id="Img14" alt="" src="../images/blue_button_bg.gif" /></td>

            <td><img id="Img15" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img16" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img17" alt="" src="../images/blue_button_bg.gif" /></td>
        </tr>
        <tr>
            <td>
                ne1</td>
            <td >
                99</td>

            <td>
                <img id="Img18" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                <img id="Img19" alt="" src="../images/blue_button_bg.gif" /></td>

            <td>
                <img id="Img20" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                 <img id="Img21" alt="" src="../images/blue_button_bg.gif" /></td>

            <td><img id="Img22" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img23" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img24" alt="" src="../images/blue_button_bg.gif" /></td>
        </tr>
        <tr>
            <td>
                ne1</td>
            <td >
                99</td>

            <td>
                <img id="Img25" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                <img id="Img26" alt="" src="../images/blue_button_bg.gif" /></td>

            <td>
                <img id="Img27" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                 <img id="Img28" alt="" src="../images/blue_button_bg.gif" /></td>

            <td><img id="Img29" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img30" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img31" alt="" src="../images/blue_button_bg.gif" /></td>
        </tr>
        <tr>
            <td>
                ne1</td>
            <td >
                99</td>

            <td>
                <img id="Img32" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                <img id="Img33" alt="" src="../images/blue_button_bg.gif" /></td>

            <td>
                <img id="Img34" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                 <img id="Img35" alt="" src="../images/blue_button_bg.gif" /></td>

            <td><img id="Img36" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img37" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img38" alt="" src="../images/blue_button_bg.gif" /></td>
        </tr>
        <tr>
            <td>
                ne1</td>
            <td >
                99</td>

            <td>
                <img id="Img39" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                <img id="Img40" alt="" src="../images/blue_button_bg.gif" /></td>

            <td>
                <img id="Img41" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                 <img id="Img42" alt="" src="../images/blue_button_bg.gif" /></td>

            <td><img id="Img43" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img44" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img45" alt="" src="../images/blue_button_bg.gif" /></td>
        </tr>
        <tr>
            <td>
                ne1</td>
            <td >
                99</td>

            <td>
                <img id="Img46" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                <img id="Img47" alt="" src="../images/blue_button_bg.gif" /></td>

            <td>
                <img id="Img48" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                 <img id="Img49" alt="" src="../images/blue_button_bg.gif" /></td>

            <td><img id="Img50" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img51" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img52" alt="" src="../images/blue_button_bg.gif" /></td>
        </tr>
        <tr>
            <td>
                ne1</td>
            <td >
                99</td>

            <td>
                <img id="Img53" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                <img id="Img54" alt="" src="../images/blue_button_bg.gif" /></td>

            <td>
                <img id="Img55" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                 <img id="Img56" alt="" src="../images/blue_button_bg.gif" /></td>

            <td><img id="Img57" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img58" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img59" alt="" src="../images/blue_button_bg.gif" /></td>
        </tr>
        <tr>
            <td>
                ne1</td>
            <td >
                99</td>

            <td>
                <img id="Img60" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                <img id="Img61" alt="" src="../images/blue_button_bg.gif" /></td>

            <td>
                <img id="Img62" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                 <img id="Img63" alt="" src="../images/blue_button_bg.gif" /></td>

            <td><img id="Img64" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img65" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img66" alt="" src="../images/blue_button_bg.gif" /></td>
        </tr>
        <tr>
            <td>
                ne1</td>
            <td >
                99</td>

            <td>
                <img id="Img67" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                <img id="Img68" alt="" src="../images/blue_button_bg.gif" /></td>

            <td>
                <img id="Img69" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                 <img id="Img70" alt="" src="../images/blue_button_bg.gif" /></td>

            <td><img id="Img71" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img72" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img73" alt="" src="../images/blue_button_bg.gif" /></td>
        </tr>
        <tr>
            <td>
                ne1</td>
            <td >
                99</td>

            <td>
                <img id="Img74" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                <img id="Img75" alt="" src="../images/blue_button_bg.gif" /></td>

            <td>
                <img id="Img76" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                 <img id="Img77" alt="" src="../images/blue_button_bg.gif" /></td>

            <td><img id="Img78" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img79" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img80" alt="" src="../images/blue_button_bg.gif" /></td>
        </tr>
        <tr>
            <td>
                ne1</td>
            <td >
                99</td>

            <td>
                <img id="Img81" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                <img id="Img82" alt="" src="../images/blue_button_bg.gif" /></td>

            <td>
                <img id="Img83" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                 <img id="Img84" alt="" src="../images/blue_button_bg.gif" /></td>

            <td><img id="Img85" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img86" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img87" alt="" src="../images/blue_button_bg.gif" /></td>
        </tr>
        <tr>
            <td>
                ne1</td>
            <td >
                99</td>

            <td>
                <img id="Img88" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                <img id="Img89" alt="" src="../images/blue_button_bg.gif" /></td>

            <td>
                <img id="Img90" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                 <img id="Img91" alt="" src="../images/blue_button_bg.gif" /></td>

            <td><img id="Img92" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img93" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img94" alt="" src="../images/blue_button_bg.gif" /></td>
        </tr>
        <tr>
            <td>
                ne1</td>
            <td >
                99</td>

            <td>
                <img id="Img95" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                <img id="Img96" alt="" src="../images/blue_button_bg.gif" /></td>

            <td>
                <img id="Img97" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                 <img id="Img98" alt="" src="../images/blue_button_bg.gif" /></td>

            <td><img id="Img99" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img100" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img101" alt="" src="../images/blue_button_bg.gif" /></td>
        </tr>
        <tr>
            <td>
                ne1</td>
            <td >
                99</td>

            <td>
                <img id="Img102" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                <img id="Img103" alt="" src="../images/blue_button_bg.gif" /></td>

            <td>
                <img id="Img104" alt="" src="../images/blue_button_bg.gif"/></td>
            <td>
                 <img id="Img105" alt="" src="../images/blue_button_bg.gif" /></td>

            <td><img id="Img106" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img107" alt="" src="../images/blue_button_bg.gif" /></td>
            <td><img id="Img108" alt="" src="../images/blue_button_bg.gif" /></td>
        </tr>
    </table>
    <div>
    </div>

</body>
</html>

 

转载于:https://www.cnblogs.com/xuruhong/p/3271747.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值