首页 文章

宽度为100%的HTML表格,在tbody中没有td wdth的垂直滚动

提问于
浏览
0

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

Tried Scenario-Issue

如果您需要其他详细信息,请让我填写您的信息 .

我试过多个帖子 . 在 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>

1 回答

  • 2

    取消注释表格宽度

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

    请尝试this

    调整大小时,单元格会相应地响应 .

相关问题