gpt4 book ai didi

jquery - 给 td 固定宽度,如果宽度超过屏幕则滚动

转载 作者:行者123 更新时间:2023-11-28 15:04:37 24 4
gpt4 key购买 nike

我有一个表格,最初有 7 列和 4 行。我想要固定宽度到 td 和 th。挑战是我在表中动态添加列和行,我成功了,因为我面临的问题是我无法控制 td、th 的宽度。最初我给 table、thead 和 tbody 设置 100% 的宽度,给 td、th 设置 15% 的宽度。

这是我的 CSS:

    table {
width: 100%;
overflow:scroll;
}
thead {
width: 100%;
overflow:scroll;
}
tbody {
width: 100%;
overflow:scroll;
}
td{
width: 15%;
}
th{
width: 15%;
}

这是我的表格:

<section class="panel">
<div class="panel-body">
<div class="row">
<div class="table-responsive">
<table border="1px" class="fixed">
<thead>
<tr>
<th rowspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
</tr>
<tr>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
</tr>
</thead>
<tbody>
<tr>
<th rowspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
</tr>
<tr>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
</tr>
<tr>
<th rowspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
</tr>
<tr>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
</tr>
<tr>
<th rowspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
</tr>
<tr>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
</tr>
</tbody>
</table>
</div>
</div>
</div>

This is the output I am getting now

我想要的是给 td、th 15% 的宽度,如果 td、th 的总宽度超过 100,那么滚动条应该出现并且 th、td 的宽度没有变化。

请帮忙。

最佳答案

CSS

 table {
display: block;
overflow: scroll;
white-space: nowrap;
}
th div
{
width:150px; //give here your approximate width
}

将 html 更改为

<section class="panel">
<div class="panel-body">
<div class="row">
<div class="table-responsive">
<table border="1px" class="fixed">
<thead>
<tr>
<th rowspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
</tr>
<tr>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
</tr>
</thead>
<tbody>
<tr>
<th rowspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
</tr>
<tr>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
</tr>
<tr>
<th rowspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
</tr>
<tr>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
</tr>
<tr>
<th rowspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
</tr>
<tr>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
</tr>
</tbody>
</table>
</div>
</div>
</div>

关于jquery - 给 td 固定宽度,如果宽度超过屏幕则滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49705372/

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