2011-12-15 3 views
0

이미지가 중첩 된 TD가 있습니다. 부모에게 ID를 추가하고 TD를 제거하려고합니다. 다음 코드는 내부에 중첩 된 이미지를 기반으로 TD를 제거하지만 제거되기 전에 상위 테이블에 ID를 추가하려고합니다. 어떤 제안?jQuery가 부모 테이블에 ID를 추가 한 다음 TD를 제거합니다.

$('td img[src="/v/vspfiles/templates/cyberfront/images/RBox_Border_Left_Top.gif"]').parent().remove(); 
+0

이 ID를 추가하고 삭제 ... 문제는 무엇인가? –

+0

상위 테이블에 ID를 추가하려고합니다. – henryaaron

답변

2
$('td img[src="/v/vspfiles/templates/cyberfront/images/RBox_Border_Left_Top.gif"]').parent().closest("table").attr('id','newId'); 

$('td img[src="/v/vspfiles/templates/cyberfront/images/RBox_Border_Left_Top.gif"]').parent().remove(); 
+0

근무 감사 ... – henryaaron

4

closestend 사용. closest은 선택자와 일치하는 가장 가까운 조상 요소를 찾고 end은 이전 선택 항목으로 돌아갑니다.

그래서 ...

$('td img[src="/v/vspfiles/templates/cyberfront/images/RBox_Border_Left_Top.gif"]') 
    .parent() // go to the td 
     .closest('table') // go to the table 
      .prop('id', 'foobar') // set the table's id property 
     .end() // go back to the td 
     .remove(); // remove it 
+0

'.end()'의 유용함 +1 –

+0

'.end()'는 제가 지금까지 알지 못했던 새로운 기능 중 하나라고 생각합니다. 답을 나누기를위한 환호. –

+0

이것이 작동하지 않는 이유를 모르겠습니다 ... 페이지의 다른 JavaScript도 손상되었습니다. – henryaaron

관련 문제