2011-09-19 3 views
-1
<table> 

    <tr><td>First Row</td><td>2 Row</td><td>1 last</td></tr> 
    <tr><td>Middle Row</td><td>2 Row</td><td>2 last</td></tr> 
    <tr><td>Last Row</td><td>2 Row</td><td>3 last</td></tr> 

    </table> 


$('tr').each(function(index) { 
    $("td:last").css({backgroundColor: 'yellow', fontWeight: 'bolder'}); 
    }); 

라이브 : http://jsfiddle.net/DxVG5/1/: 테이블의 마지막

이유없이 단지 지난 3 일,하지만 마지막 마지막 1과 2에? 내가 어떻게 고칠 수 있니?

+0

아마도 tr td : last ??? – sanders

+1

정확하게 "마지막"이 무엇을 의미하는지 알고 있습니까? – Bakudan

+0

$ ('tr td : last-child') –

답변

1

td의 컨텍스트를 추가해야합니다. 'for'변수를 사용할 수 있도록 이미 for 루프에 있습니다. 이 문맥에서 'this'는 현재 반복하고있는 tr과 같습니다. 사용하기 때문에

$('tr').each(function(index) { 
    $("td:last",this).css({backgroundColor: 'yellow', fontWeight: 'bolder'}); 
}); 
2

TD이 마지막 인 컨텍스트를 지정하십시오. 귀하의 경우는 TR입니다. jQuery 생성자에 두 번째 인수를 전달하면됩니다.

$('tr').each(function(index) { 
    // ------------| 
    //    | here 
    $("td:last", this).css({backgroundColor: 'yellow', fontWeight: 'bolder'}); 
}); 
1

당신은 당신이 단지 $(this).find()를 사용하거나 당신의 선택에서 두 번째 인수로 this를 추가하여, 현재 TR 내부 TD의를 통해 찾고 있는지 확인해야합니다.

$('tr').each(function(index) { 
    $("td:last",this).css({backgroundColor: 'yellow', fontWeight: 'bolder'}); 
}); 
0

이 내가 업데이트 한

$('tr').each(function(index) { 
    $("td:last", this).css({backgroundColor: 'yellow', fontWeight: 'bolder'}); 
}); 
1

당신의 바이올린을 시도 : 당신은 세계 $('td:last') 확인했다 http://jsfiddle.net/nslr/DxVG5/4/

, 다음과 같이 수행하는 특정 행에 대한 안 하나 (내 .each())

$(this).find('td:last').... 
3

귀하의 예제는 각 tr의 마지막 td을 얻을 것이다 : 마지막 문자 그대로 table의 마지막 td을 가져옵니다. 대신이 시도 :

$('tr td:last-child').css({ 
    backgroundColor: 'yellow', 
    fontWeight: 'bolder' 
}); 
1

을 할당 tr

$('tr').each(function(index) { 
    $("td:last-child").css({backgroundColor: 'yellow', fontWeight: 'bolder'}); 
    }); 

확인의 last-childhttp://jsfiddle.net/ucCQH/5/

관련 문제