IE,谷歌,遨游,火狐兼容的萬能合并單元格方法
// tb:要合并單元格的表格;stacol:開始合并列;endcol:結束合并列;bool:合并方式(true代表stacol與endcol之間合并; false代表兩邊和并);num標記列(防止某些信息因為內容相同而合并。eg,姓名相同而編號不同的兩個人) |
02 |
function uniteTable(tb,stacol,endcol,bool,num) |
03 |
{ var end; |
04 |
var i=0; |
05 |
var j=0; |
06 |
var rowCount=tb.rows.length; // 行數 |
07 |
var colCount=tb.rows[0].cells.length; // 列數 |
08 |
var obj1= null ; |
09 |
var obj2= null ; |
10 |
var n=5; |
11 |
var count= new Array(); |
12 |
for (i=0;i<rowCount;i++){ |
13 |
for (j=0;j<colCount;j++){ |
14 |
tb.rows[i].cells[j].id= "tb__" + i+ "_" +j; |
15 |
} |
16 |
} |
17 |
//合并標記列,防止某些信息因為內容相同而合并(不需要時,可以不用傳值) |
18 |
if ( typeof (num)!= 'undefined' ){ |
19 |
obj1=document.getElementById( "tb__0_" +(num-1).toString()); |
20 |
for ( var t=1;t<rowCount;t++){ |
21 |
obj2=document.getElementById( "tb__" +t.toString()+ "_" +(num-1).toString()); |
22 |
if (obj1.innerHTML==obj2.innerHTML){ |
23 |
obj1.rowSpan++; |
24 |
obj2.parentNode.removeChild(obj2); |
25 |
} else { |
26 |
count[t]=n; //當第一個人的名稱合并結束時,給count[t]賦值做標記 |
27 |
obj1=document.getElementById( "tb__" +t.toString()+ "_" +(num-1).toString()); } |
28 |
} |
30 |
} |
31 |
//如果endcol未定義,代碼將最終一列定義為endcol |
32 |
// if(typeof(endcol)=='undefined'){end=colCount;}else{end=endcol} |
33 |
for (i=0;i<colCount;i++){ |
34 |
//bool:合并方式,true代表兩邊合并;false代表中間合并 |
35 |
if (bool){ if (i+1<stacol||i+1>endcol){ continue ;}} |
36 |
else { if (i+1>stacol&&i+1<endcol){ continue ;} } |
37 |
obj1=document.getElementById( "tb__0_" +i.toString()); |
38 |
for (j=1;j<rowCount;j++){ |
39 |
obj2=document.getElementById( "tb__" +j.toString()+ "_" +i.toString()); |
40 |
if ( typeof (count[j])!= 'undefined' ){ |
41 |
if (count[j]!=n){ |
42 |
if (obj1.innerHtml==obj2.innerHtml){ obj1.rowSpan++; |
43 |
obj2.parentNode.removeChild(obj2); } |
44 |
else { obj1=document.getElementById( "tb__" +j.toString()+ "_" +i.toString()); } |
45 |
} else { obj1=document.getElementById( "tb__" +j.toString()+ "_" +i.toString()); } |
46 |
} |
47 |
else { if (obj1.innerHTML==obj2.innerHTML){ |
48 |
obj1.rowSpan++; |
49 |
obj2.parentNode.removeChild(obj2); |
50 |
} |
51 |
else { obj1=document.getElementById( "tb__" +j.toString()+ "_" +i.toString()); } |
52 |
} |
53 |
} |
54 |
} |
55 |
} |