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

라인의 var thirdCell 라인이 올바르게 작동하지 않습니다. 각 행에는 네 개의 자식, 모든 td 태그가 있습니다. 세 번째 셀 안에 텍스트를 넣고 싶습니다.각 행의 세 번째 셀 가져 오기

답변

5

var Rows = $("#tableid tbody tr"); 
Rows.each(function(index, element) { 
var thirdCell = $(this).find('td').eq(2).text(); 
    alert(thirdCell); 
}); 
+0

@undefined 감사합니다 .. 당신이 옳다. 인덱스 기반입니다. –

0

tr에 텍스트가 없으므로 작동하지 않습니다. TD가 있습니다. html()을 사용하여 tr의 innerHTML을 가져 오거나 실제로 텍스트가 포함 된 경우 tds에 text()를 사용할 수 있습니다.

0

는 0부터 시작하는 인덱스

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

각 행의 모든 ​​셀을 얻을 수 아래 같은 것을 시도하거나 당신은 원래의 선택에 그것을 얻을 수

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

Third.each(function(index, element) { 
    var thirdCell = $(this).text(); 
    alert(thirdCell); 
}); 
1

this은 문자열이 아니며 jquery 객체이기 때문에 건물을 만들 때 추가 할 수 없습니다. 새로운 선택자. 이처럼 할 수있는 :

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

http://jsfiddle.net/2URV7/

관련 문제