gpt4 book ai didi

javascript - 如何匹配具有固定标题的滚动表中的列

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

我正在尝试创建一个带有固定标题的滚动表。问题是 <thead> 之间的列和 <tbody>没有正确匹配。我的问题是如何让列在具有固定标题的滚动表中匹配。

下面是 fiddle ,只需点击“添加问题”按钮 3 次,然后滚动表格:

http://jsfiddle.net/6rCyH/2/

下面是表格的html:

<table id="qandatbl" align="center">
<thead class="tblhead">
<tr>
<th class="qid">Question No</th>
<th class="question">Question</th>
<th class="optandans">Option and Answer</th>

</tr>
</thead>
<tbody class="tblbody">
</tbody>
</table>

下面是控制表格和每一列的主要CSS:

body{
font-size:85%;
}

.extratd{
border:1px black solid;
border-collapse:collapse;
}

.qid {
min-width:3%;
max-width:3%;
font-weight:bold;
border:1px black solid;
border-collapse:collapse;
}

.question {
min-width:14%;
max-width:14%;
border:1px black solid;
border-collapse:collapse;
}

.question textarea {
min-width:auto;
max-width:auto;
resize:none;
height:100%;
font-size:100%;
}

.noofanswers{
min-width:15%;
max-width:15%;
padding-top:5%;
padding-bottom:5%;
}

.optandans{
min-width:30%;
max-width:30%;
border:1px black solid;
border-collapse:collapse;
}

.option{
min-width:100%;
max-width:100%;
padding-top:5%;
padding-bottom:5%;
}

.answer {
min-width:100%;
max-width:100%;
padding-top:5%;
padding-bottom:5%;
}

.tblbody{
background-color: #ddd;
height: 400px;
overflow: auto;
}

.tblhead, .tblbody {
display: block;
}

最佳答案

结果 enter image description here

使用以下 CSS

<style type="text/css">
.qid {
width:15%;
}

.question {
width:30%;
}

.extratd {
/*
* width will be the remaining of its parent.
*/
}
table {
border-collapse:collapse;
}
td, th {
border:1px solid black;
/*
* In case the long word will affect the width of TD
*/
word-wrap:break-word;
word-break:break-all;
}
thead {
/*
* minus the scollbar's width from THEAD
*/
margin-right:12px;
}
thead, tbody {
/*
* Override the default display table-head-group, table-row-group.
* If not, the height will be computed with table layout rendering algorithm
* by browser
*/
display:block;
}
tbody {
/*
* let the TABLE BODY part scroll
*/
height:400px;
overflow:scroll;
}
tbody td {
/* In case the children in the TD will change its width which
* set by CSS implicitly, such as 15%, 30%
*/
overflow:hidden;
}
textarea {
/* to override the default width of text area in case it will
* go out of TD */
width:100%;
}
</style>

查看内联评论。这个解决方案有个问题,我们必须知道滚动条的宽度,所以兼容性不好。

<table>
<thead>
<tr>
<th class="qid">Question No</th>
<th class="question">Question</th>
<th class="extratd">Option and Answer</th>
</tr>
</thead>
<tbody>
<tr align="center" class="optionAndAnswer">
<td class="qid">1</td>
<td class="question">
<textarea class="textAreaQuestion" name="questionText[]" value=""></textarea>
</td>
<td class="extratd">
<div class="option">
1. Option Type:
<br>
<input type="text" readonly="readonly" class="gridTxtRow maxRow" name="gridValues[]" value="">
<span class="showGrid" href="#">[Open Grid]</span>
</div>
<div class="noofanswers">
2. Number of Answers:
<br>
<span style="display: none;" class="naRow string" name="numberAnswer[]" value="">Only 1 Answer</span>
<input type="text" onchange="getButtons()" onkeypress="return isNumberKey(event)" onkeyup="numberKeyUp(this)" style="display: block;" class="numberAnswerTxtRow answertxt" name="numberAnswer[]" value="">
</div>
<div class="answer">
3. Answer:
<br>
</div>
</td>
</tr>
</tbody>
</table>

关于javascript - 如何匹配具有固定标题的滚动表中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14007696/

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