JS實現(xiàn)可改變列寬的table實例
更新時間:2013年07月02日 16:51:10 作者:
本文為大家詳細(xì)介紹下通過JS實現(xiàn)可改變列寬的table,具體的思路及代碼如下,感興趣的朋友可以參考下哈,希望對大家有所幫助
復(fù)制代碼 代碼如下:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="gbk">
<title>table</title>
</head>
<body>
<table id="tb_1" cellspacing="0" cellpadding="2" width="100%" border="1">
<tbody>
<tr align="center" bgcolor="#dcdcdc">
<td style="width:100px;">用戶編號</td>
<td>試用時間</td><td>轉(zhuǎn)正時間</td><td>性別</td><td>姓名拼音</td>
<td>生日時間</td><td>民族</td><td>身高</td>
</tr>
<tr>
<td>2000001</td>
<td>1997-3-13</td><td>1997-3-13</td><td>1</td><td>WZJ</td>
<td>1965-3-13</td><td>漢</td><td>171</td>
</tr>
<tr>
<td>2000045</td>
<td>2001-2-15</td><td>2001-3-15</td><td>0</td><td>WY</td>
<td>1978-8-5</td><td>漢</td><td>162</td>
</tr>
<tr>
<td>2000046</td>
<td>2001-2-23</td><td>2001-3-23</td><td>0</td><td>LQ</td>
<td>2001-2-23</td><td>漢</td><td>171</td>
</tr>
</tbody>
</table>
<script type="text/javascript">
var tTD; //用來存儲當(dāng)前更改寬度的Table Cell,避免快速移動鼠標(biāo)的問題
var table = document.getElementById("tb_1");
for (j = 0; j < table.rows[0].cells.length; j++) {
table.rows[0].cells[j].onmousedown = function () {
//記錄單元格
tTD = this;
if (event.offsetX > tTD.offsetWidth - 10) {
tTD.mouseDown = true;
tTD.oldX = event.x;
tTD.oldWidth = tTD.offsetWidth;
}
//記錄Table寬度
//table = tTD; while (table.tagName != ‘TABLE') table = table.parentElement;
//tTD.tableWidth = table.offsetWidth;
};
table.rows[0].cells[j].onmouseup = function () {
//結(jié)束寬度調(diào)整
if (tTD == undefined) tTD = this;
tTD.mouseDown = false;
tTD.style.cursor = 'default';
};
table.rows[0].cells[j].onmousemove = function () {
//更改鼠標(biāo)樣式
if (event.offsetX > this.offsetWidth - 10)
this.style.cursor = 'col-resize';
else
this.style.cursor = 'default';
//取出暫存的Table Cell
if (tTD == undefined) tTD = this;
//調(diào)整寬度
if (tTD.mouseDown != null && tTD.mouseDown == true) {
tTD.style.cursor = 'default';
if (tTD.oldWidth + (event.x - tTD.oldX)>0)
tTD.width = tTD.oldWidth + (event.x - tTD.oldX);
//調(diào)整列寬
tTD.style.width = tTD.width;
tTD.style.cursor = 'col-resize';
//調(diào)整該列中的每個Cell
table = tTD; while (table.tagName != 'TABLE') table = table.parentElement;
for (j = 0; j < table.rows.length; j++) {
table.rows[j].cells[tTD.cellIndex].width = tTD.width;
}
//調(diào)整整個表
//table.width = tTD.tableWidth + (tTD.offsetWidth – tTD.oldWidth);
//table.style.width = table.width;
}
};
}
</script>
</body>
</html>
相關(guān)文章
js實現(xiàn)的GridView即表頭固定表體有滾動條且可滾動
實現(xiàn)GridView,表頭固定,表體有滾動條且可滾動,下面有個不錯的示例,希望對大家有所幫助2014-02-02JavaScript設(shè)置彈出式獨立窗口頁面和window的方法舉例詳解
window.open是網(wǎng)頁中經(jīng)常遇到的彈出窗口代碼,不是網(wǎng)絡(luò)中比較反感的那類彈出代碼,下面這篇文章主要給大家介紹了關(guān)于JavaScript設(shè)置彈出式獨立窗口頁面和window的方法,需要的朋友可以參考下2024-01-01KnockoutJS 3.X API 第四章之click綁定
click綁定主要作用是用于DOM元素被點擊時調(diào)用相關(guān)JS函數(shù)。這篇文章主要介紹了KnockoutJS 3.X API 第四章之click綁定,感興趣的朋友一起看看吧2016-10-10