Finding the count of <td> Jquery

旧城冷巷雨未停 提交于 2019-12-19 05:46:29

问题


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.


回答1:


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.




回答2:


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", $("td:first")).length



回答3:


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(){});




回答4:


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:

var itemsCount = $(".PrintTable > tbody > tr > td, .PrintTable > tr > td").length;

Example: http://jsfiddle.net/H2JWS/




回答5:


In general, if you want to count the number of td's in a specific row, you can do this..

$(function(){
  count = $("table tr").children("td").index()+1; 
});



回答6:


var rowCount = $('#myTable tr').length;




回答7:


var originalTable = $(remove_copy_drargelement).closest('table').clone();

var tds = $(originalTable).children('tbody').children('tr').children('td').length;


来源:https://stackoverflow.com/questions/4728907/finding-the-count-of-td-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!