效果圖:拖動帶顏色位置上的豎槓,可以左右拖動表格寬度,上下寬度一起 改變
缺點:拖動點極其精確,想拖動一次要有耐心
再帖代碼:
Html代碼
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
.resizeDivClass
{
position:absolute;
background-color:gray;
width:1;
z-index:1;
left:1;
cursor:e-resize;
}
.td1 {
font-size: 12px;
white-space:nowrap;
overflow:hidden;
text-overflow:ellipsis;
color:#0000ff;
pos:expression (this.offsetLeft*1+this.offsetWidth*1+tab.offsetLeft);
}
</style>
<script language=javascript>
function MouseDownToResize(obj){
obj.mouseDownX=event.clientX;
objobj.pareneTdW=obj.parentElement.offsetWidth;
obj.pareneTableW=tab.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;
tab.style.width=obj.pareneTableW*1+event.clientX*1- obj.mouseDownX;
for (var i=1; i<=tab.rows(0).cells.length;i++){
var obj = eval("mydiv"+i);
objobj.style.left = obj.parentElement.offsetLeft*1+obj.parentElement.offsetWidth*1+tab.off setLeft;//obj.parentElement.style.pos;
}
}
}
function MouseUpToResize(obj){
obj.releaseCapture();
obj.mouseDownX=0;
}
function init(){
var tempStr = "";
for (var i=1; i<=tab.rows(0).cells.length;i++){
tempStr = '<div id="mydiv'+i+'" class="resizeDivClass" onmousedown="MouseDownToResize(this);" onmousemove="MouseMoveToResize(this);" onmouseup="MouseUpToResize (this);"></div>';
tab.rows(0).cells(i-1).innerHTML = tab.rows(0).cells(i- 1).innerHTML+tempStr;
var obj = eval("mydiv"+i);
objobj.style.left = obj.parentElement.style.pos;
}
}
</script>
</head>
<body onload="init()">
<table id=tab STYLE="table-layout:fixed;" >
<tr bgcolor=cccccc >
<td valign=top class="td1" ><nobr>改變table的列寬度改 </nobr></td>
<td valign=top class="td1" ><nobr>改變table的列寬度改 </nobr></td>
<td valign=top class="td1" ><nobr>改變table的列寬度改 </nobr></td>
</tr>
<tr>
<td class="td1"><nobr>改變table的列寬度 </nobr></td>
<td class="td1"><nobr>改變table的列寬度 </nobr></td>
<td class="td1"><nobr>改變table的列寬度 </nobr></td>
</tr>
</table>
</body>
</html>