gpt4 book ai didi

javascript - 滚动时固定 html 表头

转载 作者:太空狗 更新时间:2023-10-29 13:49:58 25 4
gpt4 key购买 nike

我有以下表结构:

<table>   
<thead>
<tr>
<th colspan="4">Current</th>
<th colspan="4">New/Requested</th>
</tr>
<tr>
<th nowrap="nowrap">RSD &nbsp;&nbsp;&nbsp;&nbsp;</th>
<th nowrap="nowrap">CRSD &nbsp;&nbsp;&nbsp;&nbsp;</th>
<th nowrap="nowrap">MSD &nbsp;&nbsp;&nbsp;&nbsp;</th>
<th nowrap="nowrap">Open QTY &nbsp;&nbsp;&nbsp;&nbsp;</th>
<th nowrap="nowrap">CRD &nbsp;&nbsp;&nbsp;&nbsp;</th>
<th nowrap="nowrap">CRSD &nbsp;&nbsp;&nbsp;&nbsp;</th>
<th nowrap="nowrap">MSD &nbsp;&nbsp;&nbsp;&nbsp;</th>
<th nowrap="nowrap">Open QTY &nbsp;&nbsp;&nbsp;&nbsp;</th>
<th nowrap="nowrap">Action</th>
<th nowrap="nowrap">Reason</th>
<th nowrap="nowrap">Action Code Status </th>
</tr>
<tbody>
<tr>
<td></td>
<td></td>
.....plenty of rows
</tr>
</tbody>
</thead>
</table>

我正在尝试修复标题,以便在我向下滚动时它保持可见。我看了this post但无法理解。我怎样才能做到这一点?

最佳答案

为了实现我的目标(如问题所述),我编写了以下代码-

这是我写的插件。

(function ($) {
$.fn.scrollbarTable = function (i) {
var o = {};
if (typeof (i) == 'number') o.height = i;
else if (typeof (i) == 'object') o = i;
else if (typeof (i) == 'undefined') o = {
height: 300
}
return this.each(function () {
var $t = $(this);
var w = $t.width();
$t.width(w - function (width) {
var parent, child;
if (width === undefined) {
parent = $('<div style="width:50px;height:50px;overflow:auto"><div style="height:50px;"></div></div>').appendTo('body');
child = parent.children();
width = child.innerWidth() - child.height(99).innerWidth();
parent.remove();
}
return width;
}());
var cols = [];
var tableCols = [];
$t.find('thead th,thead td').each(function () {
cols.push($(this).width());
});
$t.find('tr:eq(1) th,thead td').each(function () {
tableCols.push($(this).width());
});
var $firstRow = $t.clone();
$firstRow.find('tbody').remove();
$t.find('thead').remove();
$t.before($firstRow);
$firstRow.find('thead th,thead td').each(function (i) {
$(this).attr('width', cols[i]);
});
$t.find('tr:first th,tr:first td').each(function (i) {
$(this).attr('width', tableCols[i]);
});
var $wrap = $('<div>');
$wrap.css({
width: w,
height: o.height,
overflow: 'auto'
});
$t.wrap($wrap);
})
};
}(jQuery));

使用方法:

$(document).ready(function(){
$('table#tabss').scrollbarTable();
}

希望它能对某个地方的人有所帮助..

无论如何感谢你们所有人的支持...:)

关于javascript - 滚动时固定 html 表头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6534284/

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