在HTML中创建带滚动条的table

本文介绍了一种在不同浏览器中实现表格垂直滚动的兼容性方案。通过对比单一表格与双表格的方法,解决了Firefox与IE8之间的显示问题。采用双表格虽然存在对齐难点,但在两种浏览器下均能稳定工作。


                <div style="float:left;width:100%;">
                    <table class="life-table" style="border:4px solid #E78F28;" cellspacing="0">
                        <thead>
                        <tr>
                            <th class="life-title" width="35px"></th>
                            <th class="life-title" width="150px">9/2 周三</th>
                            <th class="life-title" width="150px">9/3 周四</th>
                            <th class="life-title" width="150px">9/4 周五</th>
                            <th class="life-title" width="13px"></th>
                        </tr>
                        </thead>
                       
                        <tbody style="overflow-y:scroll;overflow-x:hidden;height:100px;">
                        <tr>
                            <td class="life-title" height="22px">00:00</td>
                            <td align="center" style="background:red;color:white;">浏览网页</td>
                            <td align="center" style="background:yellow;">看书</td>
                            <td align="center" style="background:#DEECFF;"></td>
                        </tr>
                        <tr>
                            <td class="life-title" width="15px" height="22px">01:00</td>
                            <td align="center" style="background:#DEECFF;"></td>
                            <td align="center" style="background:#DEECFF;"></td>
                            <td align="center" style="background:green;color:white;">coding</td>
                        </tr>
                        <tr>
                            <td class="life-title" width="15px" height="22px">02:00</td>
                            <td align="center" style="background:#DEECFF;"></td>
                            <td align="center" style="background:blue;color:white;">学英语</td>
                            <td align="center" style="background:#DEECFF;"></td>                           
                        </tr>
                        <tr>
                            <td class="life-title" width="15px" height="22px">03:00</td>
                            <td align="center" style="background:#DEECFF;"></td>                           
                            <td align="center" style="background:green;color:white;">数据库</td>
                            <td align="center" style="background:#DEECFF;"></td>
                        </tr>
                        <tr>
                            <td class="life-title" width="15px" height="22px">00:00</td>
                            <td align="center" style="background:red;color:white;">浏览网页</td>
                            <td align="center" style="background:yellow;">看书</td>
                            <td align="center" style="background:#DEECFF;"></td>
                        </tr>
                        <tr>
                            <td class="life-title" width="15px" height="22px">01:00</td>
                            <td align="center" style="background:#DEECFF;"></td>
                            <td align="center" style="background:#DEECFF;"></td>
                            <td align="center" style="background:green;color:white;">coding</td>
                        </tr>
                        <tr>
                            <td class="life-title" width="15px" height="22px">02:00</td>
                            <td align="center" style="background:#DEECFF;"></td>
                            <td align="center" style="background:blue;color:white;">学英语</td>
                            <td align="center" style="background:#DEECFF;"></td>                           
                        </tr>
                        <tr>
                            <td class="life-title" width="15px" height="22px">03:00</td>
                            <td align="center" style="background:#DEECFF;"></td>                           
                            <td align="center" style="background:green;color:white;">数据库</td>
                            <td align="center" style="background:#DEECFF;"></td>
                        </tr>
                        </tbody>
                    </table>
                </div>

 

评:上述代码在Firefox里能work,但是在IE8不行。下面的代码使用两个表格,在Firefox和IE8中均正常。使用两个表格的缺点是,不容易将表头和内容对齐


                <div style="float:left;width:100%;height:auto;">
                    <table class="life-table" cellspacing="0" cellpadding="0">
                        <tr>
                            <th class="life-title" width="35px"></th>
                            <th class="life-title" width="150px">9/2 周三</th>
                            <th class="life-title" width="150px">9/3 周四</th>
                            <th class="life-title" width="150px">9/4 周五</th>
                            <td class="life-title" width="15px"></td>
                        </tr>
                    <tr><td colspan="5">
                    <div style="overflow-y:scroll;height:100px;">
                        <table cellspacing="0">                       
                        <tr>
                            <td class="life-title" height="22px">00:00</td>
                            <td align="center" style="background:red;color:white;">浏览网页</td>
                            <td align="center" style="background:yellow;">看书</td>
                            <td align="center" style="background:#DEECFF;"></td>
                        </tr>
                        <tr>
                            <td class="life-title" width="15px" height="22px">01:00</td>
                            <td align="center" style="background:#DEECFF;"></td>
                            <td align="center" style="background:#DEECFF;"></td>
                            <td align="center" style="background:green;color:white;">coding</td>
                        </tr>
                        <tr>
                            <td class="life-title" width="15px" height="22px">02:00</td>
                            <td align="center" style="background:#DEECFF;"></td>
                            <td align="center" style="background:blue;color:white;">学英语</td>
                            <td align="center" style="background:#DEECFF;"></td>                           
                        </tr>
                        <tr>
                            <td class="life-title" width="15px" height="22px">03:00</td>
                            <td align="center" style="background:#DEECFF;"></td>                           
                            <td align="center" style="background:green;color:white;">数据库</td>
                            <td align="center" style="background:#DEECFF;"></td>
                        </tr>
                        <tr>
                            <td class="life-title" width="15px" height="22px">00:00</td>
                            <td align="center" style="background:red;color:white;">浏览网页</td>
                            <td align="center" style="background:yellow;">看书</td>
                            <td align="center" style="background:#DEECFF;"></td>
                        </tr>
                        <tr>
                            <td class="life-title" width="15px" height="22px">01:00</td>
                            <td align="center" style="background:#DEECFF;"></td>
                            <td align="center" style="background:#DEECFF;"></td>
                            <td align="center" style="background:green;color:white;">coding</td>
                        </tr>
                        <tr>
                            <td class="life-title" width="15px" height="22px">02:00</td>
                            <td align="center" style="background:#DEECFF;"></td>
                            <td align="center" style="background:blue;color:white;">学英语</td>
                            <td align="center" style="background:#DEECFF;"></td>                           
                        </tr>
                        <tr>
                            <td class="life-title" width="15px" height="22px">03:00</td>
                            <td align="center" style="background:#DEECFF;"></td>                           
                            <td align="center" style="background:green;color:white;">数据库</td>
                            <td align="center" style="background:#DEECFF;"></td>
                        </tr>
                        <tr>
                            <td width="35px"></td>
                            <td width="150px"></td>
                            <td width="150px"></td>
                            <td width="150px"></td>
                        </tr>
                        </table>
                    </div>
                    </td></tr>
                    </table>
                </div>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值