【问题标题】:Scrolling property for body part of the table not working表格正文部分的滚动属性不起作用
【发布时间】:2012-03-05 13:56:40
【问题描述】:

我有一个表格,其中标题应固定在其位置和滚动内容。

我找到了滚动它们的方法,方法是使用单独的表格制作第一个标题,使用第二个表格制作正文部分,该表格位于一个 div 内,该表格具有滚动属性。

但是当列数越来越多时,对齐第一个表中的标题和第二个表中的内容变得越来越困难。

这就是为什么我不得不在 tbody 中创建一个滚动条。但它不起作用。我试图将滚动属性赋予 tbody 部分并固定它的高度,但它没有滚动。

我要纠正的代码是,

<table width="900" border="1" cellspacing="0" cellpadding="0">
<thead>
  <tr>
    <th scope="col">h1</th>
    <th scope="col">h2</th>
    <th scope="col">h3</th>
    <th scope="col">h4</th>
    <th scope="col">h5</th>
    <th scope="col">h6</th>
  </tr>
  </thead>

   <tbody style="height:5px; overflow:scroll;">
  <tr>
    <td height="10">1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
  </tr>
  <tr>
    <td height="10">7</td>
    <td>8</td>
    <td>9</td>
    <td>0</td>
    <td>1</td>
    <td>2</td>
  </tr>
  <tr>
    <td height="10">3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td>7</td>
    <td>8</td>
  </tr>
  <tr>
    <td height="10">9</td>
    <td>0</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
  </tr>
  </tbody>
</table>

使用 2 个表创建固定标题的代码是

<table width="900" border="1" cellspacing="0" cellpadding="0">
<thead>
  <tr>
    <th scope="col">h1</th>
    <th scope="col">h2</th>
    <th scope="col">h3</th>
    <th scope="col">h4</th>
    <th scope="col">h5</th>
    <th scope="col">h6</th>
  </tr>
  </thead>
  </table>
 <div  style="height:50px; overflow-y:scroll; width:920px;">
<table width="900" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td height="10">1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
  </tr>
  <tr>
    <td height="10">7</td>
    <td>8</td>
    <td>9</td>
    <td>0</td>
    <td>1</td>
    <td>2</td>
  </tr>
  <tr>
    <td height="10">3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td>7</td>
    <td>8</td>
  </tr>
  <tr>
    <td height="10">9</td>
    <td>0</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
  </tr>
</table>

【问题讨论】:

    标签: html css scroll html-table


    【解决方案1】:

    这样试试

    <table width="900" border="1" cellspacing="0" cellpadding="0">
     <thead>
      <tr>
        <th scope="col">h1</th>
        <th scope="col">h2</th>
        <th scope="col">h3</th>
        <th scope="col">h4</th>
        <th scope="col">h5</th>
        <th scope="col">h6</th>
      </tr>
      </thead>
    
    <tbody>
    <tr>
      <td height="10" colspan="6">
        <div style="height:5px; overflow:scroll;">
            <!--put your new table here (content of tbody)-->
    
        </div>
    </td>
    </tr>
    

    【讨论】:

    • 感谢您考虑这个问题,我尝试了您的回答,但它产生了一些我指定的类似表格的内容。这里的标题和正文也不同
    • 你到底想要什么?你想让你的 tbody 内容可以滚动吗?
    • 我正在尝试滚动 tbody 部分,就像在第一组代码中一样。第二组中的代码正在工作,但是在添加更多行时,例如 40- 对齐变得困难......那就是y 我正在尝试为同一张表应用滚动
    • 使用 CSS 完全控制您的表格行为,管理起来很容易。
    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多