日期:2014-05-16  浏览次数:20325 次

如何锁定列
在页面上,做个table   ,有   a,b   c,....好多列,底下有一个滚动条
    如何做到   :选a列时,a列就被锁定,     拉动底下滚动条时,始终出现a列?

------解决方案--------------------
楼上都是 css 实现滴,俺给个 js 版滴,L@_@K!

稍后再贴改进版!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<title> lock column </title>
<meta name= "generator " content= "editplus " />
<meta name= "author " content= "yixianggao@126.com " />
<meta name= "keywords " content= "javascript " />
<meta name= "description " content= "for javascript region of csdn " />
<style type= "text/css ">
table {
border: #a4cdf2 1px solid;
}
td {
border: #a4cdf2 1px solid;
font-family: "courier new ";
font-size: 11pt;
}
td.locked {
position: relative;
left: expression(document.getElementById( "divTableContainer ").scrollLeft);
background-color: #ffff82;
}
</style>
</head>

<body>
<h3> 点击首行锁定列,再次点击解除锁定! </h3>
<div id= "divTableContainer " style= "width: 150px; overflow-x: scroll; ">
<table cellpadding= "0 " cellspacing= "0 " id= "tabTarget ">
<tr>
<td nowrap> Column A </td>
<td nowrap> Column B </td>
<td nowrap> Column C </td>
</tr>
<tr>
<td> R1.1 </td>
<td> R1.2 </td>
<td> R1.3 </td>
</tr>
<tr>
<td> R2.1 </td>
<td> R2.2 </td>
<td> R2.3 </td>
</tr>
<tr>
<td> R3.1 </td>
<td> R3.2 </td>
<td> R3.3 </td>
</tr>
</table>
</div>
</body>
<script type= "text/javascript ">
<!--
var oTable = document.getElementById( "tabTarget ");
for (var i=0; i <oTable.rows[0].cells.length; i++)
{
oTable.rows[0].cells[i].onmouseover = function() {
this.style.cursor = "hand ";
this.style.backgroundColor = "#BCFEC8 ";
}
oTable.rows[0].cells[i].onmouseout = function() {
this.style.backgroundColor = " ";
}
oTable.rows[0].cells[i].onclick = function() {
var currentCellIndex = this.cellIndex;
var cellClassName = " ";
if (this.className == " ")
{
cellClassName = "locked ";
}

for (var i=0; i <oTable.rows.length; i++)
{
oTable.rows[i].cells[currentCellIndex].className = cellClassName;
}
}
}
//-->
</script>
</html>