常见的数据库有这个功能,就是鼠标移到表的两个字段名之间时,鼠标变成左右方向的箭头,然后按住鼠标左键,可以改变字段的宽度。
最近看到有人在页面上,使用javascript实现了这个效果,我转过来与大家分享
[code]
<html>
<title>无标题文档</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
.bg table{
font-size:12px;
color:#000000;
}
.bg td{
font-size:12px;
color:#000000;
text-align:left;
line-height:15px;
height:20px;
}
.bg td.tit{
background-color:#e2e2e2;
color:#000;
height:17px;
text-align:center;
line-height:15px;
}
.bg td.num{
background-color:#e2e2e2;
color:#000;
text-align:right;
line-height:15px;
width:30px;
height:22px;
}
.resizeDivClass{
text-align:right;
width:1px;
margin:0px 0 0px 0;
background:#fff;
border:0px;
float:right;
cursor:e-resize;
}
</style>
<script>
function MouseDownToResize(obj){
setTableLayoutToFixed();
obj.mouseDownX=event.clientX;
obj.pareneTdW=obj.parentElement.offsetWidth;
obj.pareneTableW=theObjTable.offsetWidth;
obj.setCapture();
}
function MouseMoveToResize(obj){
if(!obj.mouseDownX) return false;
var newWidth=obj.pareneTdW*1+event.clientX*1-obj.mouseDownX;
if(newWidth>0)
{
obj.parentElement.style.width = newWidth;
theObjTable.style.width=obj.pareneTableW*1+event.clientX*1-obj.mouseDownX;
}
}
function MouseUpToResize(obj){
obj.releaseCapture();
obj.mouseDownX=0;
}
function setTableLayoutToFixed()
{
if(theObjTable.style.tableLayout=='fixed') return;
var headerTr=theObjTable.rows[0];
for(var i=0;i<headerTr.cells.length;i++)
{
headerTr.cells[i].styleOffsetWidth=headerTr.cells[i].offsetWidth;
}
for(var i=0;i<headerTr.cells.length;i++)
{
headerTr.cells[i].style.width=headerTr.cells[i].styleOffsetWidth;
}
theObjTable.style.tableLayout='fixed';
}
</script>
<body >
<table width="100%" class="bg"border=1 cellspacing=0bordercolorlight="#7b7b7b" bordercolordark="#efefef" id="theObjTable" >
<tr >
<td class="num" >序号</td>
<td class="tit" >
<span class="resizeDivClass" onMouseDown="MouseDownToResize(this);" onMouseMove="MouseMoveToResize(this);" onMouseUp="MouseUpToResize(this);"><img src="images/box1.gif" width="3" height="18"></span>
公司名称
</td>
<td class="tit" >
<span class="resizeDivClass" onMouseDown="MouseDownToResize(this);" onMouseMove="MouseMoveToResize(this);" onMouseUp="MouseUpToResize(this);"><img src="images/box1.gif" width="3" height="18"></span>
订单客户
</td>
<td class="tit" >
<span class="resizeDivClass" onMouseDown="MouseDownToResize(this);" onMouseMove="MouseMoveToResize(this);" onMouseUp="MouseUpToResize(this);"><img src="images/box1.gif" width="3" height="18"></span>
部门
</td>
<td class="tit" >
<span class="resizeDivClass" onMouseDown="MouseDownToResize(this);" onMouseMove="MouseMoveToResize(this);" onMouseUp="MouseUpToResize(this);"><img src="images/box1.gif" width="3" height="18"></span>
业务员
</td>
<td class="tit" >
<span class="resizeDivClass" onMouseDown="MouseDownToResize(this);" onMouseMove="MouseMoveToResize(this);" onMouseUp="MouseUpToResize(this);"><img src="images/box1.gif" width="3" height="18"></span>
交款方式
</td>
</tr>
<tr>
<td class="num" >1</td>
<td ><a href="http://www.aspprogram.cn" target="_blank">asp编程网</a></td>
<td >aspprogram</td>
<td >设计部</td>
<td >asp</td>
<td >现金</td>
</tr>
<tr>
<td class="num" >2</td>
<td ><a href="http://www.sina.com.cn" target="_blank">新浪</a></td>
<td >sina</td>
<td >软件部</td>
<td >PHP</td>
<td >现金</td>
</tr>
<tr>
<td class="num" >3</td>
<td ><a href="http://www.baidu.com" target="_blank">百度</a></td>
<td >baidu</td>
<td >网络部</td>
<td >JSP</td>
<td >现金</td>
</tr>
<tr>
<td class="num" >4</td>
<td ><a href="http://www.google.cn" target="_blank">google</a></td>
<td >google</td>
<td >技术部</td>
<td >.net</td>
<td >现金</td>
</tr>
<tr>
<td class="num" >5</td>
<td ><a href="http://www.yahoo.com.cn" target="_blank">Yahoo</a></td>
<td >yahoo</td>
<td >广告部</td>
<td >C#</td>
<td >现金</td>
</tr>
</table>
</body>
</html>
[/code]