gpt4 book ai didi

html - 如何使 's height of position:fixed column follow its content' s 高度?

转载 作者:行者123 更新时间:2023-11-28 15:17:35 25 4
gpt4 key购买 nike

目前,我的页面中有一个表格,我正在尝试卡住第一列。使用 position: fixed 卡住列工作正常对于那个特定的列。

但是还有一个问题。当内容超过一行时,<tr>不会根据其内容扩展其高度。那么,有什么解决办法吗?

这是我的演示: https://jsfiddle.net/yusrilmaulidanraji/ckfdubsf/121/

#table-wrapper {
width: 95%;
float: left;
overflow-x: scroll;
background: #ddd;
}

table {
background: #fff;
width: 1200px;
text-align: center;
overflow: hidden;
position: relative;
}

table thead tr th {
width: 15em;
}

table thead tr th:first-child,
table tbody tr td:first-child {
top: auto;
left: 0.5;
position: fixed;
width: 6em;
}

table thead tr th:nth-child(2),
table tbody tr td:nth-child(2) {
padding-left: 7em;
/*to show second column behind the first*/
}
<button id="left">&larr;</button>
<button id="right">&rarr;</button>

<div id="table-wrapper">
<table border="1">
<thead>
<tr>
<th>Heading1</th>
<th>Heading2</th>
<th>Heading3</th>
<th>Heading4</th>
<th>Heading5</th>
</tr>
</thead>
<tbody>
<tr>
<td>1<br/>asdasdada</td>
<td>12</td>
<td>13</td>
<td>14</td>
<td>15</td>
</tr>
<tr>
<td>2<br/>asdasdada<br/>asdasdada</td>
<td>22</td>
<td>23</td>
<td>24</td>
<td>25</td>
</tr>
<tr>
<td>3<br/>asdasdada</td>
<td>32</td>
<td>33</td>
<td>34</td>
<td>35</td>
</tr>
<tr>
<td>4<br/>asdasdada<br/>asdasdada<br/>asdasdada<br/>asdasdada<br/>asdasdada</td>
<td>42</td>
<td>43</td>
<td>44</td>
<td>45</td>
</tr>
<tr>
<td>5<br/>asdasdada</td>
<td>52</td>
<td>53</td>
<td>54</td>
<td>55</td>
</tr>
</tbody>
</table>

</div>

最佳答案

您可以遍历所有 td 固定元素,然后将这些高度设置为默认 td 元素。

  $('table tbody tr td:first-child').each(function(){
var height = $(this).height();
$(this).parent().find('td').height(height);
});

工作示例 https://jsfiddle.net/ckfdubsf/122/

关于html - 如何使 <tr >'s height of position:fixed column follow its content' s 高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46832756/

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