JQuery實現相同內容合併單元格

        web前端開發的時候經常會遇到要做表單的頁面或者做一些表格的效果如相同內容要同一個單元格里面顯示,一般的方法是table裏面在套table但是這種方法會增加頁面的負擔影響頁面加載速度但是如果用DIV有不好控制寫的css樣式要很多,那怎麼辦呢?我們就中和下利用JQuery來和他一個table裏面相同內容的單元格,這裏代碼跟大家分享下,希望對大家有用,如下: 

頭部JQuery代碼 

代碼如下:

<script type="text/javascript"> 
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; 
} 
}); 
}); 
}); 
} 
$(function() { 
$(“#table1″).rowspan(0);//傳入的參數是對應的列數從0開始,哪一列有相同的內容就輸入對應的列數值 
$(“#table1″).rowspan(2); 
}); 
</script>

在body裏面加入一個表格 

代碼如下:

<body> 
<table id="table1" border="1" cellpadding="5" cellspacing="0" width="300px"> 
<tr> 
<td>1</td> 
<td>2</td> 
<td>3</td> 
<td>4</td> 
</tr> 

<tr> 
<td>1</td> 
<td>f</td> 
<td>3</td> 
<td>s</td> 
</tr> 
</table> 
</body>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章