gpt4 book ai didi

html - 一个表格 宽度基于另一个表格 使用 html/css 的宽度

转载 作者:太空宇宙 更新时间:2023-11-04 01:25:38 24 4
gpt4 key购买 nike

我的代码如下。尝试为 DIV 创建一个垂直滚动条,并使我的表头固定,这是有效的。 <强> FIDDLE

但是,table2 的第一列包含动态数据,并且此单元格的宽度会根据正在填充的数据进行调整。

我需要将我的 Head1 宽度(来自 table1)设置为等于 table2 column1 的宽度。

希望有一个简单且更好的方法来实现这一点

<table id="table1">
<thead>
<tr>
<th>Head1</th>
<th>Head2</th>
<th>Head3</th>
</tr>
</thead>
</table>
<div style="max-height: 200px; overflow: auto;">
<table id="table2">
<tbody>
<tr> <td>Contains Dynamic data</td> <td>content</td> <td>content</td> </tr>
<tr> <td>content</td> <td>content</td> <td>content</td> </tr>
<tr> <td>content</td> <td>content</td> <td>content</td> </tr>
<tr> <td>content</td> <td>content</td> <td>content</td> </tr>
</tbody>
</table>
</div>

最佳答案

通过小技巧,我假设您知道列中最长的字符串是什么,如果是这样,您可以将它放到跨度中,它会“扩展”您的标题,但您会隐藏它。

可以使用javascript来实现,搜索特定列中最长的文本,并将其放在该列的头部。

const headerContainers = document.querySelectorAll('#head-row  div');

const maxLengthPerColumn = Array.from(headerContainers).map(_ => '');

Array.from(document.querySelectorAll('#test tr'))
.forEach((row) => Array.from(row.querySelectorAll('td')).forEach((td, index) => {
maxLengthPerColumn[index] = maxLengthPerColumn[index].length < td.textContent.length ? td.textContent : maxLengthPerColumn[index];
}));

maxLengthPerColumn.forEach((longText, index) => {
const span = document.createElement('span');
span.textContent = longText;
headerContainers[index].appendChild(span);
});
thead th div {
height: 1em;
overflow: hidden;
text-align: left;
}

thead th span {
font-weight: normal;
display: block;
}
<div>Page content goes here. </div>
<table>
<thead>
<tr id="head-row">
<th>
<div>Head1</div>
</th>
<th>
<div>Head2</div>
</th>
<th>
<div>Head3</div>
</th>
</tr>
</thead>
</table>
<div style="max-height: 200px; overflow: auto;">
<table id="test">
<tbody>
<tr>
<td>Contains Dynamic data</td>
<td>content</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content Longer text</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
</tbody>
</table>
</div>
<div>More Page Content.
<br/>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium
quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.
Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque
rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam
nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris
sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc</div>

关于html - 一个表格 <td> 宽度基于另一个表格 <td> 使用 html/css 的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48192971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com