合并单元格式
1.上下合并(列合并)
? ?
$.fn.MergeColumns = function() {return this.each(function() {
for (var i = $(this).find('tr:first td').size() - 1; i >= 0; i--) { //獲取表格td的數(shù)量進(jìn)行循環(huán)
var s = null;
var prevTd = null;
$(this).find('tr').each(function() {
var td = $(this).find('td').eq(i);
var s1 = td.text();
if (s1 == s) { //相同即執(zhí)行合并操作
td.hide(); //hide() 隱藏相同的td ,remove()會(huì)讓表格錯(cuò)位 此處用hide
prevTd.attr('rowspan', prevTd.attr('rowspan') ? parseInt(prevTd.attr('rowspan')) + 1 : 2); //賦值rowspan屬性
}
else {
s = s1;
prevTd = td;
}
});
}
});
}
調(diào)用:
$("#TableId").MergeColumns();調(diào)用前:
調(diào)用后:
2.左右合并(行合并)
$.fn.MergeRows = function() {return this.each(function() {
$(this).find('tr').each(function() {
var s = null;
var prevTd = null;
for (var i = 0; i < $(this).find('td').size(); i++) {
var td = $(this).find('td').eq(i);
var s1 = td.text();
if (s1 == s) { //相同即執(zhí)行合并操作
td.hide(); //hide() 隱藏相同的td ,remove()會(huì)讓表格錯(cuò)位 此處用hide
prevTd.attr('colspan', prevTd.attr('colspan') ? parseInt(prevTd.attr('colspan')) + 1 : 2); //賦值colspan屬性
}
else {
s = s1;
prevTd = td;
}
}
});
});
}
調(diào)用:
$("#TableId").MergeRows ();調(diào)用前:
調(diào)用后:
轉(zhuǎn)載于:https://www.cnblogs.com/ericyi/archive/2011/09/22/2185139.html
總結(jié)
- 上一篇: UVA 141 The Spot Gam
- 下一篇: 25个出众的Web表单范例