jQuery自定义函数

自动合并列一样的tr行,内容居中

jQuery.fn.rowspan = function(colIdx) {
    return this.each(function(){
        var that;
        rowspan = 0;
        $('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);
                    $(that).attr("align",'center');
                    $(that).css("vertical-align",'middle');
                    $(this).hide();
                } else {
                    that = this;
                }
            });
        });
    });
}

用法:

$("#table2").rowspan(0);//合并第一列
$("#table2").rowspan(1);//合并第二列
Last modification:October 19th, 2017 at 10:35 pm

Leave a Comment