Finding column index using jQuery when table contains column-spanning cells

后端 未结 5 1354
日久生厌
日久生厌 2020-12-01 12:08

Using jQuery, how can I find the column index of an arbitrary table cell in the example table below, such that cells spanning multiple columns have multiple indexes?

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 13:01

    Mine is quite similar to SolutionYogi's, minus the creation of a plugin. It took me a bit longer... but I'm still proud of it so here it is :)

    cell = $("#example2");
    var example2ColumnIndex2 = 0;
    
    cell.parent("tr").children().each(function () {
        if(cell.get(0) != this){
            var colIncrementor = $(this).attr("colspan");
            colIncrementor = colIncrementor ? colIncrementor : 1;
            example2ColumnIndex2 += parseInt(colIncrementor);
        }
    });
    console.log(example2ColumnIndex2);
    

提交回复
热议问题