table列的相同单元格合并

合并方法

//封装的一个JQuery小插件

jQuery.fn.rowspan = function(colIdx) {
        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() && $(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;
                        }
                    });
                });
            });
          }

注:(1)单元格内容相同会合并,但是相邻是空格不合并
(2)colIdx是列号

调用方法

如果要把所有的列都合并,那么使用循环去调用
trackTable是table的ID

for(var col=0;col<trackTable.rows[1].cells.length;col++){
        $("#trackTable").rowspan(col);
    }

猜你喜欢

转载自blog.csdn.net/juligang320/article/details/77676569