jQuery:如何计算表列? [英] jQuery: How to count table columns?

查看:147
本文介绍了jQuery:如何计算表列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jQuery,您如何确定一个表中有多少列?

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

<script>
    alert($('table').columnCount());
</script>

<table>
    <tr>
        <td>spans one column</td>
        <td colspan="2">spans two columns</td>
        <td colspan="3">spans three columns</td>
    <tr>
</table>

此示例中的列总数为6.如何使用jQuery确定这一点?

The total number of columns in this example is 6. How could I determine this using jQuery?

推荐答案

在这里:

jsFiddle

$(function() {
    var colCount = 0;
    $('tr:nth-child(1) td').each(function () {
        if ($(this).attr('colspan')) {
            colCount += +$(this).attr('colspan');
        } else {
            colCount++;
        }
    });
});

这篇关于jQuery:如何计算表列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆