问题描述
我有以下 HTML 结构,我想找出直接 I have the following HTML structure and I wanted to find out the length of immediate 我用来找出 td 长度的函数是 The function that I am using to find out the length of td is 我看到的结果是 0.完全没有线索.我期待 2. 任何帮助将不胜感激. The result I am seeing is 0. No clue at all. I am expecting 2. Any help will be appreciated. 我从你的 HTML 中删除了星号,并对你如何调用 I removed the asterisks out of your HTML and made some assumptions about how you're invoking 最大的变化是添加了一个 The big change was the add a 这篇关于找到<td>的计数查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!s 的长度.这是我正在使用的代码:- <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>
function getBody(element)
{
var divider=2;
var originalTable=element.clone();
var tds = $(originalTable).children('tr').children('td').length;
alert(tds);
}
推荐答案
getBody
做了一些假设,所以如果我做了任何不对,请告诉我.getBody
, so if I did anything that wasn't right, let me know.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
中.向下遍历,你会没事的..children('tbody')
. The HTML interpreter wraps the tr
s in tbody
. Traverse down into that, and you'll be fine.