利用js、css、html固定table的列头不动

1、CSS

<style type="text/css">
    #scroll_head {
        position: absolute;
        display: none;
    }
</style>

2、Javascript

<script type="text/javascript">

    //该函数在上面一个table数据加载完成后调用
    //把表头的宽度设置到会滚动的页头去
    var copyWidth = function () {
        var b = $('#data_tbody').prev().find('tr:last').find('th');
        var c = $('#scroll_head').find('tr:last').find('th');
        for (var i = 0; i < b.length; i++) {
            var newWith = b.eq(i).width();
            if ($.browser.msie) {
                newWith += 1;
            }
            c.eq(i).width(newWith);
        }
    }


    $(function () {
        $(window).scroll(function () {
            if ($('#data_tbody').length > 0) {
                var thead = $('#data_tbody').prev();
                var thOffset = thead.offset();
                var scTop = $(window).scrollTop();  //滚动条相对top的位置
                if (scTop > thOffset.top) {   //滚动条滚到thead及以下的位置,用临时的thead代替显示
                    $('#scroll_head').css('display', 'block');
                    $('#scroll_head').offset({ top: scTop, left: thOffset.left });
                }
                else {  //滚动条滚到thead上的位置,用table的原始thead显示
                    $('#scroll_head').css('display', 'none');
                }
            }
        });
    });
</script>

3、Html内容

<div >
    <table>
        @*thead内容及样式同scroll_head中的thead*@
        @*thead使用深背景色,避免滚动时和tbody内容重叠显示*@
        <thead> 
            <tr>
                @*一级标题*@
                <th class="tt1" colspan="2">一级1</th>
                <th class="tt2" colspan="5">一级2</th>
                <th class="tt3" colspan="6">一级3</th>
            </tr>
            <tr>
                @*二级标题*@
                <th >二级11</th>
                <th >二级12</th>
                <th class="tt" >二级21</th>
                <th class="tt" >二级22</th>
                <th class="tt" >二级23</th>
                <th class="tt" >二级23</th>
                <th class="tt" >二级24</th>
                <th class="tt" >二级25</th>
                <th class="tt" >二级31</th>
                <th class="tt" >二级32</th>
                <th class="tt" >二级33</th>
                <th class="tt" >二级33</th>
                <th class="tt" >二级34</th>
                <th class="tt" >二级35</th>
                <th class="tt" >二级36</th>
            </tr>
        </thead>
        <tbody >
            数据内容,在数据加载完成后调用copyWidth()函数解决兼容性
        </tbody>
    </table>
</div>
<div  >
    <table width="100%">
        <thead>  @*thead使用深背景色,避免滚动时和tbody内容重叠显示*@
            <tr>
                @*一级标题*@
                <th class="tt1" colspan="2">一级1</th>
                <th class="tt2" colspan="5">一级2</th>
                <th class="tt3" colspan="6">一级3</th>
            </tr>
            <tr>
                @*二级标题*@
                <th >二级11</th>
                <th >二级12</th>
                <th class="tt" >二级21</th>
                <th class="tt" >二级22</th>
                <th class="tt" >二级23</th>
                <th class="tt" >二级23</th>
                <th class="tt" >二级24</th>
                <th class="tt" >二级25</th>
                <th class="tt" >二级31</th>
                <th class="tt" >二级32</th>
                <th class="tt" >二级33</th>
                <th class="tt" >二级33</th>
                <th class="tt" >二级34</th>
                <th class="tt" >二级35</th>
                <th class="tt" >二级36</th>
            </tr>
        </thead>
    </table>
</div>