看到以下代碼,並做了測試:
<body>
<input type=button onclick=hideCol(1) value='隱藏第 2 列'>
<input type=button onclick=showCol(1) value='顯示第 2 列'>
<div id=tableBox></div>
<script>
//--------------------------------------------------------
// 時間轉為時間戳(毫秒)
function time2stamp(){var d=new Date();return Date.parse(d)+d.getMilliseconds();}
//--------------------------------------------------------
// 創建表格
function createTable(rowsLen)
{
var str = "<table border=1>" +
"<thead>" +
"<tr>" +
"<th width=100>col1<\/th>" +
"<th width=200>col2<\/th>" +
"<th width=50>col3<\/th>" +
"<\/tr>" +
"<\/thead>" +
"<tbody>";
var arr = [];
for (var i=0; i<rowsLen; i++)
{
arr[i] = "<tr><td>" + i + "1<\/td><td>" + i + "2</td><td>" + i + "3<\/td></tr>";
}
str += arr.join("") + "</tbody><\/table>"; // 用 join() 方式快速構建字串,速度極快
tableBox.innerHTML = str; // 生成 table
}
//--------------------------------------------------------
// 隱藏/顯示指定列
function hideCol(colIdx){hideOrShowCol(colIdx, 0);}
function showCol(colIdx){hideOrShowCol(colIdx, 1);}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function hideOrShowCol(colIdx, isShow)
{
var t1 = time2stamp(); //
var table = tableBox.children[0];
var rowsLen = table.rows.length;
var lastTr = table.rows[0];
for (var i=0; i<rowsLen; i++)
{
var tr = table.rows[i];
tr.children[colIdx].style.display = isShow ? "" : "none";
}
var t2 = time2stamp();
alert("耗時:" + (t2 - t1) + " 毫秒");
}
//--------------------------------------------------------
createTable(1000); // 創建千行表格
</script>