HTML 自動(dòng)伸縮的表格Table js實(shí)現(xiàn)
更新時(shí)間:2009年04月01日 23:56:24 作者:
在開(kāi)發(fā)的過(guò)程中,表格Table有個(gè)缺陷,如果一行中某個(gè)單元格的超過(guò)一行,表格就不夠美觀了。
下面的代碼解決了這個(gè)問(wèn)題:當(dāng)表格被載入的時(shí)候,TD的寬度是原定的長(zhǎng)度,不會(huì)撐開(kāi)TD,也不會(huì)影響其他TD,點(diǎn)擊某行會(huì)按照本行所有單元格中行數(shù)最多的單元格的長(zhǎng)度伸長(zhǎng)行高。用戶體驗(yàn)很好。
【優(yōu)點(diǎn)】
1、對(duì)開(kāi)發(fā)人員指定的表格沒(méi)有任何影響;
2、使用簡(jiǎn)單;
3、被定義的表格樣式可以隨意的定制你的樣式,不對(duì)你的樣式構(gòu)成影響;
4、移植性好,擴(kuò)展性好。
【缺點(diǎn)】
目前用IE7測(cè)試正常,但不支持FireFox,工作比較忙,沒(méi)時(shí)間更正,希望網(wǎng)友更正,俺在此謝過(guò)。^_^
【使用方法】
1、將AutoTableSize.js包文件[點(diǎn)擊這兒下載源代碼]導(dǎo)入到你的web應(yīng)用目錄中;
2、引入包AutoTableSize.js,頁(yè)面body底部加入:
<script type="text/javascript" src="AutoTableSize.js"></script>
3、編寫(xiě)你的腳本調(diào)用:
new AutoTableSize(); 當(dāng)DOM對(duì)象中只有一個(gè)Table的時(shí)候不用指定Table的ID屬性;
new AutoTableSize(table); table:既可以是表格的ID屬性,也可以是表格對(duì)象;
源碼AutoTableSize.js
/**
* @ version: 1.0
* @ author:Xing,Xiudong
* @ email: xingxiudong[at]gmail.com
* @ index: http://blog.csdn.net/xxd851116
* @ date: 2009.04.01 愚人節(jié)
* @ desciption: AutoTableSize
*/
function AutoTableSize(table) {
table = table || document.getElementsByTagName("table")[0];
this.table = typeof(table) == "String" ? document.getElementById("table") : table;
this.init();
}
AutoTableSize.prototype.init = function() {
autoTableSize = this;
var lastClickRowIndex;
var clickCount = 0;
for (var i = 0; i < this.table.rows.length; i++) {
var maxRowHeight = 0;
var tds = this.table.rows[i].cells;
if (tds.length == 0) continue;
for (var j = 0; j < tds.length; j++) {
maxRowHeight = maxRowHeight > tds[j].offsetHeight ? maxRowHeight : tds[j].offsetHeight;
var innerDiv = document.createElement("div");
innerDiv.style.height = Number(this.table.style.fontSize.substring(0, this.table.style.fontSize.length - 2)) + 1 + "px";
innerDiv.style.overflow = "hidden";
innerDiv.style.margin = "0";
innerDiv.style.padding = "0";
innerDiv.style.border = "0";
innerDiv.innerHTML = tds[j].innerHTML;
tds[j].innerHTML = "";
tds[j].appendChild(innerDiv);
}
this.table.rows[i].maxHeight = maxRowHeight;
this.table.rows[i].onmouseover = function(){this.style.backgroundColor = "#DAE9FE";}
this.table.rows[i].onmouseout = function() {this.style.backgroundColor = "#FFF";}
this.table.rows[i].onclick = function() {
if (this.rowIndex == lastClickRowIndex) {
if (clickCount % 2 == 0) {
autoTableSize.showTR(this.rowIndex);
} else {
autoTableSize.hideTR(this.rowIndex);
}
clickCount++;
return;
}
autoTableSize.hideTR(lastClickRowIndex);
autoTableSize.showTR(this.rowIndex);
lastClickRowIndex = this.rowIndex;
clickCount++;
}
}
}
AutoTableSize.prototype.hideTR = function(index) {
if (!Number(index)) return;
tds = this.table.rows[index].cells;
for (var i = 0; i < tds.length; i++) {
tds[i].firstChild.style.height = Number(this.table.style.fontSize.substring(0, this.table.style.fontSize.length - 2)) + 1 + "px";
}
}
AutoTableSize.prototype.showTR = function(index) {
if (!Number(index)) return;
tds = this.table.rows[index].cells;
for (var i = 0; i < tds.length; i++) {
tds[i].firstChild.style.height = this.table.rows[index].maxHeight - 2 * this.table.getAttribute("cellpadding");
}
}
【優(yōu)點(diǎn)】
1、對(duì)開(kāi)發(fā)人員指定的表格沒(méi)有任何影響;
2、使用簡(jiǎn)單;
3、被定義的表格樣式可以隨意的定制你的樣式,不對(duì)你的樣式構(gòu)成影響;
4、移植性好,擴(kuò)展性好。
【缺點(diǎn)】
目前用IE7測(cè)試正常,但不支持FireFox,工作比較忙,沒(méi)時(shí)間更正,希望網(wǎng)友更正,俺在此謝過(guò)。^_^
【使用方法】
1、將AutoTableSize.js包文件[點(diǎn)擊這兒下載源代碼]導(dǎo)入到你的web應(yīng)用目錄中;
2、引入包AutoTableSize.js,頁(yè)面body底部加入:
<script type="text/javascript" src="AutoTableSize.js"></script>
3、編寫(xiě)你的腳本調(diào)用:
new AutoTableSize(); 當(dāng)DOM對(duì)象中只有一個(gè)Table的時(shí)候不用指定Table的ID屬性;
new AutoTableSize(table); table:既可以是表格的ID屬性,也可以是表格對(duì)象;
源碼AutoTableSize.js
復(fù)制代碼 代碼如下:
/**
* @ version: 1.0
* @ author:Xing,Xiudong
* @ email: xingxiudong[at]gmail.com
* @ index: http://blog.csdn.net/xxd851116
* @ date: 2009.04.01 愚人節(jié)
* @ desciption: AutoTableSize
*/
function AutoTableSize(table) {
table = table || document.getElementsByTagName("table")[0];
this.table = typeof(table) == "String" ? document.getElementById("table") : table;
this.init();
}
AutoTableSize.prototype.init = function() {
autoTableSize = this;
var lastClickRowIndex;
var clickCount = 0;
for (var i = 0; i < this.table.rows.length; i++) {
var maxRowHeight = 0;
var tds = this.table.rows[i].cells;
if (tds.length == 0) continue;
for (var j = 0; j < tds.length; j++) {
maxRowHeight = maxRowHeight > tds[j].offsetHeight ? maxRowHeight : tds[j].offsetHeight;
var innerDiv = document.createElement("div");
innerDiv.style.height = Number(this.table.style.fontSize.substring(0, this.table.style.fontSize.length - 2)) + 1 + "px";
innerDiv.style.overflow = "hidden";
innerDiv.style.margin = "0";
innerDiv.style.padding = "0";
innerDiv.style.border = "0";
innerDiv.innerHTML = tds[j].innerHTML;
tds[j].innerHTML = "";
tds[j].appendChild(innerDiv);
}
this.table.rows[i].maxHeight = maxRowHeight;
this.table.rows[i].onmouseover = function(){this.style.backgroundColor = "#DAE9FE";}
this.table.rows[i].onmouseout = function() {this.style.backgroundColor = "#FFF";}
this.table.rows[i].onclick = function() {
if (this.rowIndex == lastClickRowIndex) {
if (clickCount % 2 == 0) {
autoTableSize.showTR(this.rowIndex);
} else {
autoTableSize.hideTR(this.rowIndex);
}
clickCount++;
return;
}
autoTableSize.hideTR(lastClickRowIndex);
autoTableSize.showTR(this.rowIndex);
lastClickRowIndex = this.rowIndex;
clickCount++;
}
}
}
AutoTableSize.prototype.hideTR = function(index) {
if (!Number(index)) return;
tds = this.table.rows[index].cells;
for (var i = 0; i < tds.length; i++) {
tds[i].firstChild.style.height = Number(this.table.style.fontSize.substring(0, this.table.style.fontSize.length - 2)) + 1 + "px";
}
}
AutoTableSize.prototype.showTR = function(index) {
if (!Number(index)) return;
tds = this.table.rows[index].cells;
for (var i = 0; i < tds.length; i++) {
tds[i].firstChild.style.height = this.table.rows[index].maxHeight - 2 * this.table.getAttribute("cellpadding");
}
}
相關(guān)文章
原生JavaScript實(shí)現(xiàn)動(dòng)態(tài)省市縣三級(jí)聯(lián)動(dòng)下拉框菜單實(shí)例代碼
像平時(shí)購(gòu)物選擇地址時(shí)一樣,通過(guò)選擇的省動(dòng)態(tài)加載城市列表,通過(guò)選擇的城市動(dòng)態(tài)加載縣區(qū)列表,從而可以實(shí)現(xiàn)省市縣的三級(jí)聯(lián)動(dòng),下面使用原生的JavaScript來(lái)實(shí)現(xiàn)這個(gè)功能,需要的朋友參考下吧2016-02-02
js報(bào)錯(cuò) Object doesn''t support this property or method的原因分析
運(yùn)行js是出現(xiàn)Object doesn't support this property or method 錯(cuò)誤的可能原因分析。2011-03-03
JS去除字符串的空格增強(qiáng)版(可以去除中間的空格)
之前發(fā)了不少了去除字符串空格的代碼,但都是去除開(kāi)始與結(jié)尾的,下面的這段代碼可以去除中間的。2009-08-08
JavaScript中使用replace結(jié)合正則實(shí)現(xiàn)replaceAll的效果
JavaScript?中使用?replace?達(dá)到?replaceAll的效果,其實(shí)就用利用的正則的全局替換。2010-06-06
JavaScript數(shù)組常用方法實(shí)例講解總結(jié)
這篇文章主要介紹了JavaScript數(shù)組及常見(jiàn)方法,結(jié)合實(shí)例形式總結(jié)分析了JavaScript數(shù)組的基本獲取、添加、刪除、排序、翻轉(zhuǎn)等相關(guān)操作技巧,需要的朋友可以參考下2021-09-09
javascript用DIV模擬彈出窗口_窗體滾動(dòng)跟隨
可滾動(dòng)跟隨彈出框效果代碼,非常實(shí)用的應(yīng)用于網(wǎng)絡(luò)廣告2008-09-09
ie7+背景透明文字不透明超級(jí)簡(jiǎn)單的實(shí)現(xiàn)方法
這篇文章主要介紹了ie7+背景透明文字不透明超級(jí)簡(jiǎn)單的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2014-01-01

