js table 固定第一行
时间: 2023-05-26 22:03:05 浏览: 55
您可以使用CSS中的“position: sticky”属性将表的第一行固定在屏幕顶部。以下是一个例子:
HTML:
```
<table id="myTable">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
<td>Row 1, Column 3</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
<td>Row 2, Column 3</td>
</tr>
<tr>
<td>Row 3, Column 1</td>
<td>Row 3, Column 2</td>
<td>Row 3, Column 3</td>
</tr>
<!-- add more rows here -->
</tbody>
</table>
```
CSS:
```
#myTable thead tr {
position: sticky;
top: 0;
background-color: #fff; /* optional */
}
```
这将使表的头固定在屏幕顶部,而表的其余部分可以向下滚动。请注意,“position: sticky”属性仅在支持它的浏览器中可用,如Chrome、Firefox、Safari等。在不支持的浏览器中,第一行将表现为通常的滚动行为。
阅读全文
相关推荐















