查找< td>的计数jQuery的 [英] Finding the count of <td> Jquery

查看:90
本文介绍了查找< td>的计数jQuery的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML结构,我想知道立即< td> s 的长度。这里是我使用的代码: - $ / $>

 < table class =PrintTable> 
< tr>
**< td> **
< table>
< thead>
< tr>< th>交易类型< / th>< / tr>
< / thead>
< tbody>
< tr>
< td>名称< / td>
< / tr>
< tr>
< td>年龄< / td>
< / tr>
< / tbody>
< / table>
< / td>
**< td> **
< table>
< thead>
< tr>< th> 2006< / th>< / tr>
< / thead>
< tbody>
< tr>
< td> Andi< / td>
< / tr>
< tr>
< td> 25< / td>
< / tr>
< / tbody>
< / table>
< / td>

< / tr>
< / table>

我用来找出td长度的函数是

  function getBody(element)
{
var divider = 2;
var originalTable = element.clone();
var tds = $(originalTable).children('tr')。children('td')。length;
alert(tds);


}

我看到的结果是0。根本没有线索。我期待着2.任何帮助将不胜感激。

解决方案

我从您的HTML中删除了星号并假设了你正在调用 getBody ,所以如果我做了任何不正确的事情,请告诉我。

代码: http://jsfiddle.net/27ygP/

  function getBody(element){
var divider = 2;
var originalTable = element.clone();
var tds = $(originalTable).children('tbody')。children('tr')。children('td')。length;
alert(tds);
}

getBody($('table.PrintTable'));

最大的变化是添加 .children('tbody')。 HTML解释器将 tr s封装在 tbody 中。向下进入,你会没事的。


I have the following HTML structure and I wanted to find out the length of immediate <td>s. here is the code that I am using:-

<table class="PrintTable">
    <tr>
      **<td>**
        <table>
            <thead>
                <tr><th>Type Of Transaction</th></tr>
            </thead>
            <tbody>
                <tr>
                    <td>Name</td>
                </tr>
                <tr>
                    <td>Age</td>
                </tr>
            </tbody>
        </table>
      </td>
      **<td>**
        <table>
            <thead>
                <tr><th>2006</th></tr>
            </thead>
            <tbody>
                <tr>
                    <td>Andi</td>
                </tr>
                <tr>
                    <td>25</td>
                </tr>
            </tbody>
        </table>
      </td>

    </tr>
</table>

The function that I am using to find out the length of td is

function getBody(element)
{
    var divider=2;
    var originalTable=element.clone();
    var tds = $(originalTable).children('tr').children('td').length;
    alert(tds);


}

The result I am seeing is 0. No clue at all. I am expecting 2. Any help will be appreciated.

解决方案

I removed the asterisks out of your HTML and made some assumptions about how you're invoking getBody, so if I did anything that wasn't right, let me know.

Code: http://jsfiddle.net/27ygP/

function getBody(element) {
    var divider = 2;
    var originalTable = element.clone();
    var tds = $(originalTable).children('tbody').children('tr').children('td').length;
    alert(tds);
}

getBody($('table.PrintTable'));

The big change was the add a .children('tbody'). The HTML interpreter wraps the trs in tbody. Traverse down into that, and you'll be fine.

这篇关于查找&lt; td&gt;的计数jQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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