【问题标题】:HTML table with 100% width, with vertical scroll inside tbody without td wdth具有 100% 宽度的 HTML 表格,在 tbody 内垂直滚动,没有 td 宽度
【发布时间】:2018-02-22 21:16:34
【问题描述】:

我已经尝试过 Hashem Qolami 在帖子 HTML table with 100% width, with vertical scroll inside tbody 中发布的解决方案,但遇到了问题。

如果您需要更多详细信息,请让我填写。

我尝试了多个帖子。在tbody 中,我到处都发现td 的固定宽度。但我需要动态宽度,根据tbody中的数据。

当有不同的 js、css 部分时,它在站点中工作。 但是当我将所有内容组合到一个 html 时不起作用。 我的Html图片:HTML Image

// Change the selector if needed
var $table = $('table.scroll'),
  $bodyCells = $table.find('tbody tr:first').children(),
  colWidth;

// Adjust the width of thead cells when window resizes
$(window).resize(function() {
  // Get the tbody columns width array
  colWidth = $bodyCells.map(function() {
    return $(this).width();
    alert("hii");
  }).get();

  // Set the width of thead columns
  $table.find('thead tr').children().each(function(i, v) {
    $(v).width(colWidth[i]);
  });
}).resize(); // Trigger resize handler
table.scroll {
  /* width: 100%; */
  /* Optional */
  /* border-collapse: collapse; */
  border-spacing: 0;
  border: 2px solid black;
}

table.scroll tbody,
table.scroll thead {
  display: block;
}

thead tr th {
  height: 30px;
  line-height: 30px;
  /* text-align: left; */
}

table.scroll tbody {
  height: 100px;
  overflow-y: auto;
  overflow-x: hidden;
}

tbody {
  border-top: 2px solid black;
}

tbody td,
thead th {
  /* width: 20%; */
  /* Optional */
  border-right: 1px solid black;
  /* white-space: nowrap; */
}

tbody td:last-child,
thead th:last-child {
  border-right: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<table class="scroll">
  <thead>
    <tr>
      <th>Head 1</th>
      <th>Head 2</th>
      <th>Head 3</th>
      <th>Head 4</th>
      <th>Head 5</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Content 1</td>
      <td>Content 2</td>
      <td>Content 3</td>
      <td>Content 4</td>
      <td>Content 5</td>
    </tr>
    <tr>
      <td>Content 1</td>
      <td>Lorem ipsum dolor sit amet.</td>
      <td>Content 3</td>
      <td>Content 4</td>
      <td>Content 5</td>
    </tr>
    <tr>
      <td>Content 1</td>
      <td>Content 2</td>
      <td>Content 3</td>
      <td>Content 4</td>
      <td>Content 5</td>
    </tr>
    <tr>
      <td>Content 1</td>
      <td>Content 2</td>
      <td>Content 3</td>
      <td>Content 4</td>
      <td>Content 5</td>
    </tr>
    <tr>
      <td>Content 1</td>
      <td>Content 2</td>
      <td>Content 3</td>
      <td>Content 4</td>
      <td>Content 5</td>
    </tr>
    <tr>
      <td>Content 1</td>
      <td>Content 2</td>
      <td>Content 3</td>
      <td>Content 4</td>
      <td>Content 5</td>
    </tr>
    <tr>
      <td>Content 1</td>
      <td>Content 2</td>
      <td>Content 3</td>
      <td>Content 4</td>
      <td>Content 5</td>
    </tr>
  </tbody>
</table>

【问题讨论】:

    标签: jquery html css vertical-scrolling


    【解决方案1】:

    取消注释您的表格宽度

    table.scroll {
      width: 100%;
      /* Optional */
      /* border-collapse: collapse; */
      border-spacing: 0;
      border: 2px solid black;
     }
    

    请尝试this

    调整大小时,单元格会做出相应的响应。

    【讨论】:

    • 感谢您的帮助。但是当我尝试运行整个 html 时,我不确定我有什么问题。它不起作用..当我尝试使用这些其他代码模板源时,它可以工作。您能否建议..我在编辑中添加了我的 html 截图作为图像..
    最近更新 更多