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

后端 未结 5 1357
日久生厌
日久生厌 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 12:48

    Slightly modified version is here: http://jsfiddle.net/Lijo/uGKHB/13/

    //INDEX
    alert ( GetNonColSpanIndex ('Type'));
    
    function GetNonColSpanIndex(referenceHeaderCellValue) {
    
        var selectedCell = $("th").filter(function (i) {
            return ($.trim($(this).html()  )) == referenceHeaderCellValue;
    
        });
    
        alert(selectedCell.html());
    
        var allCells = $(selectedCell).parent('tr').children();
        var normalIndex = allCells.index($(selectedCell));
        var nonColSpanIndex = 0;
    
        allCells.each(
        function (i, item) {
            if (i == normalIndex)
                return false;
    
            var colspan = $(selectedCell).attr('colspan');
            colspan = colspan ? parseInt(colspan) : 1;
            nonColSpanIndex += colspan;
        }
        );
    
        return nonColSpanIndex;
    };
    

提交回复
热议问题