Modify table structure (merge cells) with jQuery

前端 未结 3 1818
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-07 04:01

I have a table

9:00task1
10:00
3条回答
  •  礼貌的吻别
    2021-01-07 05:00

    If you must use jQuery, then this works:

    $('#ts10')
        .text($('#ts11').text())
        .attr('rowspan','2')
        .closest('tbody')
        .find('#ts11')
        .remove();​
    

    JS Fiddle demo.

    Or, somewhat more concisely:

    $('#ts10')
        .text($('#ts11').remove().text())
        .attr('rowspan','2');​
    

    JS Fiddle demo.

    And a slightly more...useful approach, which will merge cells of adjacent rows with the class of two:

    $('tr td.two').each(
        function(){
            var that = $(this),
                next = that.parent().next().find('.two');
            if (next.length){
                that
                    .text(next.remove().text())
                    .attr('rowspan','2');
            }
        });
    

    JS Fiddle demo.

提交回复
热议问题