jQuery: How to count table columns?

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

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



15条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-28 13:48

    Pass in a table with something like $('foo#table') or $('table:first')

    function getColumnCount(e) { //Expects jQuery table object
        var c= 0;
        e.find('tbody tr:first td').map(function(i,o) { c += ( $(o).attr('colspan') === undefined ? 1 : parseInt($(o).attr('colspan')) ) } );
        return c;
    }
    

提交回复
热议问题