我希望能够:
TD
行相同。我对JavaScript很陌生,所以我不知道自己缺少什么。滚动部分正在工作,但不在jsfiddle中。如何使TH
宽度与相同TD
?
编辑1:
码
<html>
<head>
<style>
.outerDIV {
position: relative;
padding-top: 30px; //height of your thead
}
.innerDIV {
overflow: auto;
max-height: 200; //the actual scrolling container
}
table thead {
display: block;
position: absolute;
top: 0;
left: 0;
}
table tbody {
display: block;
}
</style>
<script>
function scrollingTableSetThWidth(tableId)
{
var table = document.getElementById(tableId);
ths = table.getElementsByTagName('th');
tds = table.getElementsByTagName('td');
if(ths.length > 0) {
for(i=0; i < ths.length; i++) {
ths[i].style.width = getCurrentComputedStyle(tds[i], 'width');
}
}
}
function getCurrentComputedStyle(element, attribute)
{
var attributeValue;
if (window.getComputedStyle)
{ // class A browsers
var styledeclaration = document.defaultView.getComputedStyle(element, null);
attributeValue = styledeclaration.getPropertyValue(attribute);
} else if (element.currentStyle) { // IE
attributeValue = element.currentStyle[vclToCamelCases(attribute)];
}
return attributeValue;
}
</script>
</head>
<body>
<div class="outerDIV">
<div class="innerDIV">
<table border="1">
<thead>
<tr>
<div><th>Column1</th><th>Column2</th><th>Column3</th></div>
</tr>
</thead>
<tbody>
<tr>
<td>Line1Cell1</td><td>Line1Cell2</td><td>Line1Cell3</td>
</tr>
<tr>
<td>Line2Cell1</td><td>Line2Cell2</td><td>Line2Cell3</td>
</tr>
<tr>
<td>Line3Cell1</td><td>Line3Cell2</td><td>Line3Cell3</td>
</tr>
<tr>
<td>Line4Cell1</td><td>Line4Cell2</td><td>Line4Cell3</td>
</tr>
<tr>
<td>Line5Cell1</td><td>Line5Cell2</td><td>Line5Cell3</td>
</tr>
<tr>
<td>Line6Cell1</td><td>Line6Cell2</td><td>Line6Cell3</td>
</tr>
<tr>
<td>Line7Cell1</td><td>Line7Cell2</td><td>Line7Cell3</td>
</tr>
<tr>
<td>Line8Cell1</td><td>Line8Cell2</td><td>Line8Cell3</td>
</tr>
<tr>
<td>Line9Cell1</td><td>Line9Cell2</td><td>Line9Cell3</td>
</tr>
<tr>
<td>Line10Cell1</td><td>Line10Cell2</td><td>Line10Cell3</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
您需要给width
元素设置一个absolute
位置。
为了更好看,宽度应减小约1 em(滚动条的平均宽度)。
如果所有列的宽度均匀分布,则可以执行以下操作:http : //jsfiddle.net/MVxZb/9/
.outerDIV {
position: relative;
padding-top: 30px;
display:inline-block;
}
.innerDIV {
overflow: auto;
max-height: 150px;
padding-right:1.1em;
margin-right:-1.1em;
}
table thead {
display:table;
width:100%;
border:solid 1px gray;
box-sizing:border-box;
position: absolute;
border-bottom:0;
top: 0;
left: 0;
right:0;
}
table-layout:fixed;
如果未设置th和tds的宽度,则可以用来在列上均匀分布宽度。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句