2011-10-19 3 views
0

나는 내 테이블 아래에 href 링크가 있고 그 링크를 클릭하여 테이블 행을 조작 할 수 있기를 원하지만 나는 그것을 얻을 수 없다! 여기

내가 뭔가하고 싶은 내 HTML

<div> 
    <div> <a href="#remove" class="removelink" > remove </a> </div> 

    <table> 
    <tr> <td></td> </tr> 
    </table> 
</div> 

입니다 : 내가

$(this).parent().siblings('table') 

에 의해 표를 얻을 수 있습니다

$('.removelink').click(function(){ 
$(this).parent().siblings('table tr:last').remove(); 
}) 

을하지만 난 뭔가에 의해 행을 얻을 질수 like

$(this).parent().siblings('table tr') 
+0

귀하의 jQuery 코드는 잘 작동합니다. JsFiddle http://jsfiddle.net/Yd49H/ –

+0

@NullPointer를보십시오 : 이것은 어떤 이유로 든 전체 테이블을 제거합니다. –

+0

@ Felix.Thanks .. 나는 그것을 깨닫지 못했습니다. –

답변

2

당신은 table에서 tr에 도착 find를 사용할 수 있습니다

$('.removelink').click(function(){ 
    $(this).parent().siblings('table').find('tr:last').remove(); 
}); 

가 여기 working example입니다. HTML 구조가 항상 표시된대로 정확히 일치하는 경우 대신 next() 대신 약간 짧은 코드를 사용할 수 있습니다.

현재 코드의 문제점은 siblings('table tr')tr 인 형제를 찾고 tr인데 아무 것도 없습니다!

0
var $context = $(this).parent().siblings('table'); 
$("tr:last", $context); 
1

.siblings(selector)은 선택기와 일치하는 특정 요소의 모든 형제를 반환합니다.

.siblings('table tr')은 컨텍스트 요소가 형제로 tr 요소를 가지고 있지만 div 요소가없는 경우에만 무엇인가를 반환합니다.

그냥 .find를 사용

$(this).parent().siblings('table').find('tr').last() 
+0

'.remove()'를 추가하십시오 : http://jsfiddle.net/Yd49H/2/에서 결과보기 –

관련 문제