2
var Rows = $("#tableid tbody tr");
Rows.each(function(index, element) {
var thirdCell = $(this+":nth-child(3)").text();
    alert(thirdCell);
});

Something in the var thirdCell line is not working correctly. Each row has four children, all td tags. I want to get the text inside the third cell.

4

4 に答える 4

5

Try something like below,

var Rows = $("#tableid tbody tr");
Rows.each(function(index, element) {
var thirdCell = $(this).find('td').eq(2).text();
    alert(thirdCell);
});
于 2012-08-15T20:33:01.887 に答える
1

this is not a string, it's a jquery object, so it can't be appended to when you are building your new selector. You could do it like this:

var selector = $(this).selector;
var thirdCell = $(selector+":nth-child(3)").text();
alert(thirdCell);

http://jsfiddle.net/2URV7/

于 2012-08-15T20:37:26.473 に答える
0

This will not work because tr has no text. it has TDs. you can apply though html() to get the innerHTML of the tr, or use text() on tds if they actually contain text.

于 2012-08-15T20:31:18.457 に答える
0

you can get all cells from each row by 0 based index

Rows.each(function(index, element) {
    var thirdCell = $(this.cells[2]).text();
    alert(thirdCell);
});

or you can get it in original selector

var Third = $("#tableid tbody tr > td:nth-child(3)");

Third.each(function(index, element) {
    var thirdCell = $(this).text();
    alert(thirdCell);
});
于 2012-08-15T20:34:03.203 に答える