计算具有相同类别的不同表的TD数 [英] Count TD number of different tables with same classes

查看:98
本文介绍了计算具有相同类别的不同表的TD数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有两个具有相同类的表,唯一的区别是列数.

Let's say we have two tables with same classes, the only difference is the number of columns.

<table class="lives prono_live">
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
    </tr>
</table>

<table class="lives prono_live">
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
        <td>5</td>           
    </tr>
</table>

我想要一个jQuery函数,该函数可以在一个循环中为我提供每个表的TD号.

I want a jQuery function which gaves me each table's TD number in one loop.

我创建了以下代码:

$( '.lives.prono_live' ).each(function( index ) {
    alert($('.lives.prono_live td').length);
});

这段代码不好,只是部分原因.

This code is not good, just partly.

此代码的作用:

  • 显示两个值为9和9的警报框

我想要拥有的东西:

  • 显示两个值为4和5的警报框

有人知道该怎么做吗?

推荐答案

因为您的代码正在计算所有tds,因为您在循环$('.lives.prono_live td')中具有类选择器...请使用$(this)引用,它应该可以工作.

cause your code is counting all tds since you have class selector inside loop $('.lives.prono_live td')... use $(this) reference and it should work..

尝试

$( '.lives.prono_live' ).each(function( index ) {
  alert($(this).find('td').length); 
});

在这里拨弄

这篇关于计算具有相同类别的不同表的TD数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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