DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> 基於JQuery實現相同內容合並單元格的代碼
基於JQuery實現相同內容合並單元格的代碼
編輯: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>

IE效果

火狐效果

更多的前端開發可以關注 UI前端

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved