2012-08-08 7 views
4

다음과 같은 시나리오에서 jQuery를 배우려고합니다. 이를 위해 여러 가지 질문을 읽은 후 다음 jQuery를 시도했다. 그러나 그것은 작동하지 않았다jQuery 가까운 학부모 찾기

$(this).closest('.viewLogText').parent().find('.reportLog').css("display", "none"); 

시나리오 : CSS 클래스 "repeaterRecord"가 사업부에서 세 자식 DIV 요소가 있습니다

. 하위 div는 css 클래스 (repeaterIdentifier, viewLogTitle 및 reportLog)와 함께 있습니다.

이 구조체에는 두 개의 div가 있습니다 (div에는 Css 클래스 "repeaterRecord"가 있음).

enter image description here

viewLog 클래스와 DIV

은 아래와 같이된다. collapseLogIcon 이미지를 클릭하면

<div class="viewLogTitle"> 
     <div class="viewLogText"> 
      View Report Log 
     </div> 
     <div> 
      <img alt="Expand" src="Images/PlusIcon.GIF" class="expandLogIcon" /> 
      <img alt="Collapse" src="Images/MinusIcon.GIF" class="collapseLogIcon" /> 
     </div> 
    </div> 

, 나는 (전용) ("viewLogTitle"같은 수준에서) "REPORTLOG"클래스와 가장 가까운 사업부를 숨길 필요가있다. jQuery를 사용하여 어떻게 할 수 있습니까?

작업 예 업데이트 :

http://jsfiddle.net/Lijo/L9w4F/11/http://jsfiddle.net/Lijo/L9w4F/8/http://jsfiddle.net/Lijo/L9w4F/12/

참조 :

  1. Efficient, concise way to find next matching sibling?

  2. ,536,913 63,210
  3. jquery select siblings 'until'

답변

1

당신은 siblings() 방법을 사용할 수 있습니다 :

$(this).closest('.viewLogText').siblings('.reportLog').hide() 

당신은 또한 .css("display", "none");

5

당신은 가장 가까운 div을 찾아 .siblings()를 사용할 수 있습니다.

API HERE

2

I가 사용 좋을 것 : 필터가 next() 방법 ('.reportLog')으로 전달하는 것이

$(this).closest('.viewLogTitle').next('.reportLog').hide(); 

주를 viewLogTitle 요소의 다음 형제 요소 영향된다는 것을 의미 해당 셀렉터와 일치하는 경우에만입니다. .viewLogTitle의 다음 형제가 일 때 항상이 대상이면 (HTML은 변경되지 않음) 필터는 필요하지 않으므로 생략 할 수 있습니다.

또는, 그들은 항상 연속적으로 수행하지 않습니다 (하지만이 '가까운'은 항상 영향을받을 수있는 하나입니다) 다음과 같은 형제 자매를 들어, :

$(this).closest('.viewLogTitle').nextAll('.reportLog:first').hide(); 

또는 .reportLog이 앞에있는 경우 (형제 자매 이전에 대한 .viewLogTitle) :

$(this).closest('.viewLogTitle').prevAll('.reportLog:first').hide(); 

참고 :

+0

과 동일하다 hide() 방법을 시도 할 수있어 하나의 요소 – Esailija

+0

필요하지 않습니다 next''의 필터링 선택 진실, 그것이가는 한. 그러나 필터를 전달하려는 의도는 다음 형제가 원하는 대상이 아닌 시간을 설명하는 것이 었습니다. 공정하게, 나는 그 의도를 설명해야만했다. ** 편집 됨 ** –

+1

그럴 수 없다면,'next'는 적절하지 않습니다 : P – Esailija