【问题标题】:How to create a Scrollable Table in Google Chrome如何在 Google Chrome 中创建可滚动表格
【发布时间】:2011-03-24 20:41:38
【问题描述】:

好的,这基本上是关于可滚动表格的。

如果我有一个表格元素,我可以通过使thead 固定高度和tbody 固定高度,然后将滚动设置为自动开启来使内容可滚动(通过一些javascript 的乐趣,包括使用宽度来计算滚动条)身体。

这在 Firefox 中有效,但在 google chrome 中,thead/tbody 上的高度会被忽略,整个表格会按预期增长而不是滚动。

是否有可以应用于表格的 css 解决方案以使其在 google chrome 中也能正常工作?还是我需要将标题设为一个表,将正文设为不同的表,使用 js 或 css 确保列宽并让正文表的父级滚动其内容?

【问题讨论】:

  • 那将是可怕的非语义标记。你试过设置整张桌子的高度吗?
  • 桌子上的高度作为最小高度属性。不能强迫桌子只有一定的高度。我想要一个解决方案,不需要我将表格分成两张桌子,一张用于头部,另一张用于固定列宽。

标签: javascript html css


【解决方案1】:

这是一个应该可以工作的完整解决方案(使用jQuery) 您将需要在单独的 div 元素中有两个表。每个表将具有相同的主题部分。只有 contentTable 会有 tbody 部分

<div class="scrollableTable"
    <div>
        <table class="headerTable">
            <thead>
                <tr>
                    <th>Column 1</th>
                    <th>Column 2</th>
                    <th>Column 3</th>
                </tr>
            </thead>
        </table>
    </div>
     <div style="height:240px; overflow:auto;">
        <table class="contentTable" style="width:100%; margin-top:-20px;">
            <thead>
                <tr>
                    <th>Column 1</th>
                    <th>Column 2</th>
                    <th>Column 3</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>value 1</td>
                    <td>value 2</td>
                    <td>value 3</td>
                </tr>
                <tr>
                    <td>value 4</td>
                    <td>value 5</td>
                    <td>value 6</td>
                </tr>
                ....
            </tbody>
        </table>
    </div>
</div>

然后,每次更改数据时都必须调用此函数:

ResizeWidths($('.scrollableTable'));


function ResizeWidths(div)
{
    var headerCells =  $(div).find('.headerTable thead').find('th');
    var contentCells = $(div).find('.contentTable thead').find('th');
    for(var i =0, ii = headerCells.length;i<ii;i++)
    {
        if($(headerCells[i]).width()!=$(contentCells[i]).width())
            $(headerCells[i]).width($(contentCells[i]).width());
    }
}

【讨论】:

  • 我想要一个单一表格中的解决方案,它适用于除 chrome 以外的所有浏览器,我只是想了解为什么 chrome 无法正常工作。
  • 我希望避免这样的事情,但看起来我别无选择,只能这样做。
  • 我已经在 IE7/8、Chrome 和 FF 上测试过,效果很好。我添加了两件事:1)$(div).find('.contentTable')[0].style['marginTop'] = '-' + $(contentCells[0]).height() + 'px'; 动态分配负边距顶部。 2) 同步标题和内容 div 之间水平滚动的代码。在 FF 和 Chrome 中,我使用了 onscroll 事件。在 IE 7/8 中,我添加了一个 100 毫秒的 setInterval 调用同步方法(我知道,丑陋,但有效)。
【解决方案2】:

我猜你使用的是来自以下page的脚本

为了修复 Chrome 中出现的错误,请更改以下行:

 if (document.all && document.getElementById && !window.opera) this.initIEengine();
    if (!document.all && document.getElementById && !window.opera) this.initFFengine();

到这里:

 var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
    if (is_chrome = true) {
        this.initIEengine()
    }
    else {
        if (document.all && document.getElementById && !window.opera) this.initIEengine();
        if (!document.all && document.getElementById && !window.opera) this.initFFengine();
    }

然后完美运行

【讨论】:

  • 不使用那个脚本...但是这是一个全局的东西,会在整个页面中设置一些 IE 行为吗?这感觉更接近于使用核武器杀死老鼠。仍然很高兴知道可以做一些事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-10
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 2019-06-27
  • 2020-11-06
  • 1970-01-01
相关资源
最近更新 更多