HTML 表格自动伸缩实现:基于 AutoTableSize.js 的完整指南
本文介绍如何使用 AutoTableSize.js 实现 HTML 表格行的自动伸缩功能。该脚本通过监听点击事件,动态调整单元格高度以显示完整内容,同时保持其他行不变。文章提供了详细的安装步骤、调用方法及完整源码分析,适用于需要优化表格用户体验的 Web 项目。
HTML 表格自动伸缩实现概述
以下代码解决了表格加载时的宽度固定问题:TD 宽度保持预设长度,不会因内容撑开而变形,也不影响其他 TD。当用户点击某一行时,该行会根据所有单元格中内容最多(即高度最大)的那个单元格自动伸长行高。这种机制显著提升了用户体验。
【优点】
功能特点与兼容性说明
1、对开发人员预先定义的表格结构没有任何干扰;
2、使用方式简单直观;
3、允许自定义被定义表格的样式,不会覆盖或影响原有的样式设计;
4、具有良好的移植性和扩展性。
【缺点】
目前已在 IE7 环境下测试通过,但暂不支持 FireFox 浏览器。由于工作繁忙,作者暂无时间修复兼容性问题,希望广大网友协助修正,在此先致谢。^_^
【使用方法】
安装与调用方法
1、将 AutoTableSize.js 包文件[点击这儿下载源代码]导入到你的 web 应用目录中;
2、引入 AutoTableSize.js 脚本,在页面 body 底部添加以下代码:
<script type="text/javascript" src="AutoTableSize.js"></script>
3、编写你的脚本调用:
new AutoTableSize(); 当 DOM 对象中仅包含一个 Table 时,无需指定 Table 的 ID 属性;
new AutoTableSize(table); table 参数既可以是表格的 ID 属性字符串,也可以是表格对象本身;
AutoTableSize.js 源码解析
源码 AutoTableSize.js
代码如下:
/**
* @ version: 1.0
* @ author:Xing,Xiudong
* @ email: xingxiudong[at]gmail.com
* @ index: http://blog.csdn.net/xxd851116
* @ date: 2009.04.01 愚人节
* @ 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");
}
}JS监听滚动事件:回到顶部按钮的显示与隐藏逻辑
2026-11-05 16:19:37
JavaScript怎么读写LocalStorage与SessionStorage
2026-11-05 15:40:11
anime.js 与 GSAP 对比:轻量级项目该怎么选择动画库
2026-11-05 11:24:55
flv.js 与 hls.js 在直播场景下的选型对比
2026-11-05 08:18:52
JavaScript能做什么?前端开发中的作用与应用场景解析
2026-11-04 23:17:54
