查找 <td> Jquery 的数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4728907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding the count of <td> Jquery
提问by Nrusingha
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:-
我有以下 HTML 结构,我想找出 immediate 的长度<td>s
。这是我正在使用的代码:-
<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
我用来找出 td 长度的函数是
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.
我看到的结果是 0。完全没有线索。我期待 2. 任何帮助将不胜感激。
回答by Steven
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.
我从您的 HTML 中删除了星号,并对您的调用方式做了一些假设getBody
,所以如果我做了任何不正确的事情,请告诉我。
Code: http://jsfiddle.net/27ygP/
代码: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 tr
s in tbody
. Traverse down into that, and you'll be fine.
最大的变化是添加了一个.children('tbody')
. HTML 解释器将tr
s包装在tbody
. 向下遍历,你会没事的。
回答by phillip
I think you want to use the following.
我认为您想使用以下内容。
$("td").length
UPDATE
更新
You will want to use the tr tag as the start selector and then count each td selector using first to take just the first one.
您将希望使用 tr 标记作为开始选择器,然后使用 first 计算每个 td 选择器以仅取第一个。
$("tr", $("td:first")).length
回答by Neeraj Singh
Try this:
尝试这个:
//For FFox
$(document).ready(function(){
var countTD=$("Your_Table_ID_or_Class tr:first > td").length;
});
// For webKit Browser
$(window).load(function(){
var countTD=$("Your_Table_ID_or_Class tr:first > td").length;
});
Note; If you creating dynamic table row column then use
$(document).live("click",function(){});
笔记; 如果您创建动态表行列,则使用
$(document).live("click",function(){});
回答by Uttam Grandhi
In general, if you want to count the number of td
's in a specific row, you can do this..
一般来说,如果你想计算td
特定行中's的数量,你可以这样做..
$(function(){
count = $("table tr").children("td").index()+1;
});
回答by Kalyani
var rowCount = $('#myTable tr').length;
var rowCount = $('#myTable tr').length;
回答by Peter ?rneholm
This should work:
这应该有效:
var itemsCount = $(".PrintTable > tr > td").length;
Update:
更新:
I just realized that at least Chrome inserts <tbody>
if it isn't already present, so to get cross browser support:
我刚刚意识到,<tbody>
如果Chrome不存在,至少会插入,因此要获得跨浏览器支持:
var itemsCount = $(".PrintTable > tbody > tr > td, .PrintTable > tr > td").length;
Example: http://jsfiddle.net/H2JWS/
回答by user6234155
var originalTable = $(remove_copy_drargelement).closest('table').clone();
var tds = $(originalTable).children('tbody').children('tr').children('td').length;