jQuery: How to count table columns?

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

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



15条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-28 13:40

    To circumvent the td/th issue (and also fix a potential issue where attr('colspan') was giving me strings) I went with this:

    var colspan = 0;
    $('#table').find('tr:first').children().each(function(){
        var cs = $(this).attr('colspan');
        if(cs > 0){ colspan += Number(cs); }
        else{ colspan++; }
    });
    

提交回复
热议问题