javascript-HTML表固定的标头具有最大列宽和使用jQuery的填充
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-HTML表固定的标头具有最大列宽和使用jQuery的填充,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3145字,纯文字阅读大概需要5分钟。
内容图文
![javascript-HTML表固定的标头具有最大列宽和使用jQuery的填充](/upload/InfoBanner/zyjiaocheng/658/e4f86c61fbb94a75a005e017488b011f.jpg)
我有一个带有标题的HTML表,一旦用户向下滚动,它就会用jQuery锁定到浏览器的顶部.我已经在SO和其他地方看到了很多针对此特定问题的解决方案,但是无法克服我遇到的障碍.
我已经能够使用jQuery来锁定标头了(我的< thead>中有一个< tr>包含我的列标头,并且给了< tr>一个ID“ theadTrId ”).
$(document).ready(function () {
var widths = new Array();
//create array of <th> widths
var i = 0;
$('th').each(function () {
widths[i] = $(this).width();
i++;
});
var elementPosTop = $('#theadTrId').offset().top;
$(window).scroll(function () {
SetWidths();
});
var SetWidths = function () {
var wintop = $(window).scrollTop();
if (wintop > elementPosTop) {
$('#theadTrId').css({ "position": "fixed", "top": "0" });
//<th> and <td> widths in 1st column
$('#th1').width(widths[0]);
$('#td1').width(widths[0]);
//<th> and <td> widths in 2nd column
$('#th2').width(widths[1]);
$('#td2').width(widths[1]);
//x number of other columns with same logic...
} else {
$('#theadTrId').css({ "position": "inherit" });
}
};
});
这样可以很好地工作,并且在向下滚动时将标题锁定在窗口的顶部,而在向上滚动时将其恢复为默认值,但是对我而言,我无法获得< &和<吨添加以下CSS时要对齐的列宽:
td {
max-width: 200px;
}
td, th {
padding: 10px;
}
如果我删除该样式,则所有标题及其列均会完美对齐.此样式是必需的,不能删除.表格宽度也需要设置为100%.有什么建议么?
jsFiddle这里(如果删除最后2个CSS属性,则可以看到所需的行为,在Firefox中这个问题最明显):http://jsfiddle.net/aKe6j/59/
解决方法:
试试这个(the fiddle),我认为这可以解决:
<style>
table {
width: 100%;
}
td {
background-color: #BCE6E6;
}
#theadTrId {
background-color: #6699FF;
}
#th1 {
width: 110px;
}
#th2 {
width: 235px;
}
#th3 {
width: 80px;
}
td {
max-width: 200px;
}
td, th {
padding: 10px;
}
</style>
</head>
<table border="1">
<thead>
<tr id="theadTrId">
<th id="th1">Column 1</th>
<th id="th2">Column 2</th>
<th id="th3">Column 3</th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
<script>
//loop to add 40 rows
for (var i=0;i < 41; i++)
{
var jj = 1;
$("#tbody").append("<tr><td id='td"+(jj++)+"'>Content " + i + "</td><td id='td"+(jj++)+"'>Content " + i + "</td><td id='td"+(jj++)+"'>Content " + i + "</td></tr>")
}
var widths = new Array();
//create array of <th> widths
var i = 0;
$('th').each(function () {
widths[i] = $(this).context.offsetWidth;
i++;
});
var elementPosTop = $('#theadTrId').offset().top;
$(window).scroll(function () {
SetWidths();
});
var SetWidths = function () {
var wintop = $(window).scrollTop();
if (wintop > elementPosTop) {
$('#theadTrId').css({ "position": "fixed", "top": "0" });
//use the css rulz to change also the max-width for that particular td
//it can be rolled back afther
//<th> and <td> widths in 1st column
$('#th1').css('width',widths[0]);
$('#td1').css('width',widths[0]);
$('#td1').css('max-width',widths[0]);
//<th> and <td> widths in 2nd column
$('#th2').css('width',widths[1]);
$('#td2').css('width',widths[1]);
$('#td2').css('max-width',widths[1]);
//<th> and <td> widths in 3rd column
$('#th3').css('width',widths[2]);
$('#td3').css('width',widths[2]);
$('#td3').css('max-width',widths[2]);
} else {
$('#theadTrId').css({ "position": "inherit" });
// if you need the max-width again
$('#td0').css('max-width',200);
$('#td1').css('max-width',200);
$('#td2').css('max-width',200);
}
};
</script>
内容总结
以上是互联网集市为您收集整理的javascript-HTML表固定的标头具有最大列宽和使用jQuery的填充全部内容,希望文章能够帮你解决javascript-HTML表固定的标头具有最大列宽和使用jQuery的填充所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。