jQuery: How to count table columns?

前端 未结 15 1390
天命终不由人
天命终不由人 2020-12-28 13:20

Using jQuery, how would you figure out how many columns are in a table?



15条回答
  •  囚心锁ツ
    2020-12-28 13:34

    You have to set an ID to the header row:

spans one column spans two columns spans three columns

And then you can use the following function:

function getColumnCount(headerRowId) {
    var columnCount = 0;
    $('#' + headerRowId + ' > td').each(function() {
        var colspanValue = $(this).attr('colspan');
        if (colspanValue == undefined) {
            columnCount++;
        } else {
            columnCount = columnCount + parseInt(colspanValue);
        }
    });
    return columnCount;
}

提交回复
热议问题