1. 程式人生 > >前端合併單元格

前端合併單元格

做專案的時候發現的一個前人寫的合併單元格的JQ,因為不是專業前端開發人員特此記錄下來防止以後會用到

<script type="text/javascript" defer> 
$(function(){ 
	jQuery.fn.rowspan = function(colIdx) { //封裝的一個JQuery小外掛 
		return this.each(function(){ 
			var that; 
			$('tr', this).each(function(row) { 
				$('td:eq('+colIdx+')', this).filter(':visible').each(function(col) { 
					if (that!=null && $(this).html() == $(that).html()) { 
						rowspan = $(that).attr("rowSpan"); 
						if (rowspan == undefined) { 
							$(that).attr("rowSpan",1); 
							rowspan = $(that).attr("rowSpan");
						} 
						rowspan = Number(rowspan)+1; 
						$(that).attr("rowSpan",rowspan); 
						$(this).hide(); 
					} else { 
						that = this; 
					} 
				}); 
			}); 
		}); 
	} 
	if($("#table1").length>0){
		$("#table1").rowspan(0);//傳入的引數是對應的列數從0開始,哪一列有相同的內容就輸入對應的列數值 
		$("#table1").rowspan(1); 
		$("#table1").rowspan(2); 
	}
	if($("#table2").length>0){
		$("#table2").rowspan(0);//傳入的引數是對應的列數從0開始,哪一列有相同的內容就輸入對應的列數值 
		$("#table2").rowspan(1); 
		$("#table2").rowspan(2); 
	}
	
}); 
</script>