表格合并
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>鍚堝苟琛ㄦ牸</title>
<script>
///////////////////////////////////////////////
// 鍔熻兘锛氬悎骞惰〃鏍?
// 鍙傛暟锛歵b锛嶏紞闇€瑕佸悎骞剁殑琛ㄦ牸ID
// 鍙傛暟锛歝olLength锛嶏紞闇€瑕佸鍓嶅嚑鍒楄繘琛屽悎骞讹紝姣斿锛?
// 鎯冲悎骞跺墠涓ゅ垪锛屽悗闈㈢殑鏁版嵁鍒楀拷鐣ュ悎骞讹紝colLength搴斾负2
// 缂虹渷琛ㄧず瀵瑰叏閮ㄥ垪鍚堝苟
// data: 2005.11.6
///////////////////////////////////////////////
function uniteTable(tb,objCol){
// 妫€鏌ヨ〃鏍兼槸鍚﹁鏁?
if (!checkTable(tb)) return;
var i=0;
var j=0;
var rowCount=tb.rows.length; // 琛屾暟
var colCount=tb.rows[0].cells.length; // 鍒楁暟
var obj1=null;
var obj2=null;
// 涓烘瘡涓崟鍏冩牸鍛藉悕
for (i=0;i<rowCount;i++){
for (j=0;j<colCount;j++){
tb.rows[i].cells[j].id="tb__" + i.toString() + "_" + j.toString();
}
}
// 閫愬垪妫€鏌ュ悎骞?
for (t=0;t<objCol.length;t++)
{
i=objCol[t];
obj1=document.getElementById("tb__0_"+i.toString())
for (j=1;j<rowCount;j++)
{
obj2=document.getElementById("tb__"+j.toString()+"_"+i.toString());
if (obj1.innerHTML == obj2.innerHTML)//淇敼浜嗗師鏉ョ殑innerText
{
obj1.rowSpan++;
obj2.parentNode.removeChild(obj2);
}
else
{
obj1=document.getElementById("tb__"+j.toString()+"_"+i.toString());
}
}
}
}
/////////////////////////////////////////
// 鍔熻兘锛氭鏌ヨ〃鏍兼槸鍚﹁鏁?
// 鍙傛暟锛歵b锛嶏紞闇€瑕佹鏌ョ殑琛ㄦ牸ID
// data: 2005.11.6
/////////////////////////////////////////
function checkTable(tb){
if (tb.rows.length==0) return false;
if (tb.rows[0].cells.length==0) return false;
for (var i=0;i<tb.rows.length;i++){
if (tb.rows[0].cells.length != tb.rows[i].cells.length) return false;
}
return true;
}
</script>
</head>
<body>
<table width="400" border="1" id="table1">
<tr>
<td>a</td>
<td>for</td>
<td>100</td>
<td>200</td>
</tr>
<tr>
<td>a</td>
<td>for</td>
<td>150</td>
<td>230</td>
</tr>
<tr>
<td>a</td>
<td>if</td>
<td>150</td>
<td>200</td>
</tr>
<tr>
<td>a</td>
<td>if</td>
<td>300</td>
<td>240</td>
</tr>
<tr>
<td>a</td>
<td>if</td>
<td>320</td>
<td>240</td>
</tr>
</table>
<br>
<input type="button" value="鍚堝苟琛ㄦ牸" onClick="uniteTable(table1,Array(0,1,3))">
</body>