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

问题描述

我有以下 HTML 结构,我想找出直接 s 的长度.这是我正在使用的代码:-

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>

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

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);


}

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

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

推荐答案

我从你的 HTML 中删除了星号,并对你如何调用 getBody 做了一些假设,所以如果我做了任何不对,请告诉我.

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.

代码: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 包装在 tbody 中.向下遍历,你会没事的.

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;的计数查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 11:16