Modify table structure (merge cells) with jQuery

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

I have a table

9:00task1
10:00
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-07 04:58

    This works for me. Merges cells with same text: Logic: for each cell, if next cell same text, remove next cell, increment colSpan. (Note "colSpan", not "colspan" - apparently a browser compat issue)

    $('#tblData tbody tr:first-child td').each(function(){
     var colSpan=1;
     while( $(this).text() == $(this).next().text() ){
      $(this).next().remove();
      colSpan++;
     }
    $(this).attr('colSpan',colSpan);
    });
    

提交回复
热议问题