js合并table儲存格(拼table的時候並不知道具體幾行幾列),table幾列
Sys.Application.add_load(function () { var tab = document.getElementById("ctl00_ContentPlaceHolder1_viewcontrolTT_Repeater1_ctl00_viewcontrolTT_grid"); //要合并的tableID if (!tab) { alert("未擷取到表格!"); } else { //從第二行開始,排除標題列 var startRow = 1; //迴圈記錄表格中td的內容,用來判斷td中的value是否發生了改變 var td1TempV = ""; var td2TempV = ""; var td3TempV = ""; //如果td的值相同,那麼變數加1, 否則將臨時變數加入集合中 var rowCount1 = 1; var rowCount2 = 1; var rowCount3 = 1; //得到相同內容的行數的集合www.2cto.com var totalcount1 = new Array(); var totalcount2 = new Array(); var totalcount3 = new Array(); for (var i = 1; i < tab.rows.length; i++) { //首先拿出來td的值 var td1Text = tab.rows[i].cells[0].innerText; var td2Text = tab.rows[i].cells[1].innerText; var td3Text = tab.rows[i].cells[2].innerText; //如果是第一次走迴圈,直接continue; if (i == startRow) { td1TempV = td1Text; td2TempV = td2Text; td3TempV = td3Text; continue; } //如果當前拿出來的值和出處的值相同,那麼將臨時數量加1,否則添加到集合裡面 if (td1TempV == td1Text && td1Text != null && td1Text != "") { rowCount1++; } else { totalcount1.push(rowCount1); td1TempV = td1Text; rowCount1 = 1; } if (td2TempV == td2Text && td2Text != null && td2Text != "") { rowCount2++; } else { totalcount2.push(rowCount2); td2TempV = td2Text; rowCount2 = 1; } if (td3TempV == td3Text && td3Text != null && td3Text != "") { rowCount3++; } else { totalcount3.push(rowCount3); td3TempV = td3Text; rowCount3 = 1; } //判斷是否是迴圈的最後一次,如果是最後一次那個直接將當前的數量儲存到集合裡面 if (i == tab.rows.length - 1) { totalcount1.push(rowCount1); totalcount2.push(rowCount2); totalcount3.push(rowCount3); } } //臨時變數,再迴圈中判斷是否和數組中的一項值相同 var tNum1 = 0; var tNum2 = 0; var tNum3 = 0; //注意這個迴圈是倒著來的 for (var i = tab.rows.length - 1; i >= startRow; i--) { //臨時變數,儲存td var tTd1 = tab.rows[i].cells[0]; var tTd2 = tab.rows[i].cells[1]; var tTd3 = tab.rows[i].cells[2]; tNum1++; tNum2++; tNum3++; if (tab.rows.length == 2) { continue; } //如果發現tNum和數組中最後一個值相同,那麼就可以斷定相同的td已經結束[只是其中一個] if (tNum1 == totalcount1[totalcount1.length - 1]) { //給當前td添加rowSpan屬性 tTd1.setAttribute("rowSpan", totalcount1[totalcount1.length - 1]); tTd1.setAttribute("style", "background-color:#ffffff"); //將數組的最後一個元素彈出 totalcount1.pop(); tNum1 = 0; } else { //刪除當前td tab.rows[i].removeChild(tTd1); } if (tNum2 == totalcount2[totalcount2.length - 1]) { //給當前td添加rowSpan屬性 tTd2.setAttribute("rowSpan", totalcount2[totalcount2.length - 1]); tTd2.setAttribute("style", "background-color:#ffffff"); //將數組的最後一個元素彈出 totalcount2.pop(); tNum2 = 0; } else { //刪除當前td tab.rows[i].removeChild(tTd2); } if (tNum3 == totalcount3[totalcount3.length - 1]) { //給當前td添加rowSpan屬性 tTd3.setAttribute("rowSpan", totalcount3[totalcount3.length - 1]); tTd3.setAttribute("style", "background-color:#ffffff"); //將數組的最後一個元素彈出 totalcount3.pop(); tNum3 = 0; } else { //刪除當前td tab.rows[i].removeChild(tTd3); } } }});
Html 有一個table表格我怎使用js 去合并某一個儲存格,或者某一行 或者某一列?坐等思路可以用JQ
使用HTML DOM 對象方法 組合起來使用;
1、刪除HTML元素<!DOCTYPE html><html><body><div id="div1"><p id="p1">我是P1</p><p id="p2">我是P2</p></div><script>var parent=document.getElementById("div1"); ,尋找 id="div1" 的元素:var child=document.getElementById("p2"); ,尋找 id="p1" 的 <p> 元素:parent.removeChild(child); ,從父元素中刪除子項目:</script></body></html>2、添加HTML元素<!DOCTYPE html><html><body><div id="div1"><p id="p1">我是P1</p><p id="p2">我是P2</p></div><script>var para=document.createElement("p"); ,這段代碼建立了一個新的 <p> 元素var node=document.createTextNode("我是P3"); ,這段代碼建立文本節點para.appendChild(node); ,向 <p> 元素追加文本節點var element=document.getElementById("div1"); ,尋找到一個已有的元素element.appendChild(para); ,在上面尋找到的元素中追加新元素:</script></body></html>其它的不一一說明:方法 描述getElementById() ,返回帶有指定 ID 的元素。 getElementsByTagName() ,返回包含帶有指定標籤名稱的所有元素的節點列表(集合/節點數組)。 getElementsByClassName() ,返回包含帶有指定類名的所有元素的節點列表。 appendChild() ,把新的子節點添加到指定節點。 removeChild() ,刪除子節點。 replaceChild() ,替換子節點。 insertBefore() ,在指定的子節點前面插入新的子節點。 createAttribute() ,建立屬性節點。 createElement() ,建立元素......餘下全文>>
解答:怎用JS實現Table儲存格的合并與拆分解答
上了那個網站。但沒找到你說的例子。
To51windows(海娃)、inelm(木野狐):
你們的例子都滿足不了我的要求。
那個線上編輯器還可以,但操作不方便。而且沒代碼。另外在拆分儲存格時有Bug.
Toawaysrain(絕對零度):
你提供的代碼正是我夢寐以求的。在簡單方正的表格中進行測試完全沒有問題。
但遺憾的是,在存在複雜的colSpan及rowSpan的表格中進行測試。該演算法無法完成跨行的合并。
在完成跨列的儲存格合并後,也未對影響範圍內的其它儲存格的rowSpan進行處理,這導致多次合併作業後表格錯亂。
我自己的演算法與該演算法存在差不多相同的問題。
請各位大蝦再提供些參考。感激不盡啊!