javascript - 具有可滚动行和固定标题的表格

标签 javascript html css

我有this code基于 this solution ,但它似乎对我不起作用。

我希望能够:

  • 滚动行
  • 要修复的标题
  • 标题宽度与第一个 TD 行相同。

我对 JavaScript 很陌生,所以我不知道我错过了什么。滚动部分正在工作,但不是在 jsfiddle 中。如何使 THTD 的宽度相同?

编辑 1:

代码

<html>
<head>
    <style>
        .outerDIV {
            position: relative;
            padding-top: 30px;   //height of your thead
        }
        .innerDIV {
            overflow: auto;
            max-height: 200;       //the actual scrolling container
        }
        table thead {
            display: block;
            position: absolute;
            top: 0;
            left: 0;
        }
        table tbody {
            display: block;
        }
    </style>
    <script>
        function scrollingTableSetThWidth(tableId)
        {
            var table = document.getElementById(tableId);

            ths = table.getElementsByTagName('th');
            tds = table.getElementsByTagName('td');

            if(ths.length > 0) {
                for(i=0; i < ths.length; i++) {
                    ths[i].style.width = getCurrentComputedStyle(tds[i], 'width');
                }
            }
        }

        function getCurrentComputedStyle(element, attribute)
        {
            var attributeValue;
            if (window.getComputedStyle) 
            { // class A browsers
                var styledeclaration = document.defaultView.getComputedStyle(element, null);
                attributeValue = styledeclaration.getPropertyValue(attribute);
            } else if (element.currentStyle) { // IE
                attributeValue = element.currentStyle[vclToCamelCases(attribute)];
            }
            return attributeValue;
        }
    </script>

</head>
<body>
    <div class="outerDIV">
        <div class="innerDIV">
            <table border="1">
                <thead>
                    <tr>
                        <div><th>Column1</th><th>Column2</th><th>Column3</th></div>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Line1Cell1</td><td>Line1Cell2</td><td>Line1Cell3</td>
                    </tr>
                    <tr>
                        <td>Line2Cell1</td><td>Line2Cell2</td><td>Line2Cell3</td>
                    </tr>
                    <tr>
                        <td>Line3Cell1</td><td>Line3Cell2</td><td>Line3Cell3</td>
                    </tr>
                    <tr>
                        <td>Line4Cell1</td><td>Line4Cell2</td><td>Line4Cell3</td>
                    </tr>
                    <tr>
                        <td>Line5Cell1</td><td>Line5Cell2</td><td>Line5Cell3</td>
                    </tr>
                    <tr>
                        <td>Line6Cell1</td><td>Line6Cell2</td><td>Line6Cell3</td>
                    </tr>
                    <tr>
                        <td>Line7Cell1</td><td>Line7Cell2</td><td>Line7Cell3</td>
                    </tr>
                    <tr>
                        <td>Line8Cell1</td><td>Line8Cell2</td><td>Line8Cell3</td>
                    </tr>
                    <tr>
                        <td>Line9Cell1</td><td>Line9Cell2</td><td>Line9Cell3</td>
                    </tr>
                    <tr>
                        <td>Line10Cell1</td><td>Line10Cell2</td><td>Line10Cell3</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</body>
</html>

最佳答案

滚动不起作用的原因是您的 .innerDiv max-height 属性上没有任何单位。

为了匹配表头的宽度,如果可以设置 <th> 的宽度,则不需要 javascript和 <td>通过 CSS 的单元格。

看这个:http://jsfiddle.net/MVxZb/10/

编辑:更新链接。忘记删除不需要的 javascript。

关于javascript - 具有可滚动行和固定标题的表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23790586/

相关文章:

javascript - 根据对象中的键长度/计数对对象数组进行排序

javascript - 如何在 chrome 开发工具中使用对象扩展语法

html - 如何在 BakerFramework 报亭应用程序中添加状态栏而不在内容下方留出空间?

css - 在 Rails 4.1 中为表单指定样式

javascript - 滚动后将图像固定/粘贴到顶部 - 图像立即跳到顶部?

html - 为什么不鼓励按钮导航?

Javascript,按属性提取和组织对象数组

javascript - 尝试将我的node.js应用程序上传到heroku,它说Web进程未能在启动后60秒内绑定(bind)到$PORT,进程退出,状态为137

php - 拨动开关 - 设置值

html - 更改导航栏的背景颜色