2010-12-09 2 views
1

이야기 카테고리를 나타내는 div 세트가 있습니다. 각 카테고리에는 카테고리 헤드를 포함하는 div와 스토리의 요약을 포함하는 형제 div가 있습니다.jQuery가이 div를 토글하지 않는 이유는 무엇입니까?

기본 스토리를 포함하는 카테고리를 제외하고 모든 div가 숨겨져 있습니다. 그 이야기 div의 표시 (아래 CAT2)를 밖으로 시작합니다

나는 다른 카테고리 헤드의 클릭하면, 나는 모든 종류의 헤드 된 div의 아래쪽 삼각형 아이콘을 설정하는 '확장'클래스를 제거
[ cat1 < ] 
[ cat2 V ] 
    - story2-1 
    - story2-2 
[ cat3 < ] 

, 모든 스토리 div를 숨긴 다음 클릭 된 카테고리 헤드에서 '확장'클래스를 토글하고 마지막으로 클릭 된 카테고리 헤드의 형제 인 스토리 div를 토글합니다.

카테고리 헤드를 클릭하면이 모든 것이 정상적으로 작동합니다. 현재 펼쳐진 것보다 좋습니다.

문제 : 나는 그들이 모든 붕괴 이야, 그래서 그것을 축소하려면 현재 확장 카테고리 헤드를 클릭하면는, 아무 일도 발생하지 않습니다. 또는 더 구체적으로 말하자면, 결과를 기록 할 때 이야기 div가 '표시'속성을 '차단'으로 설정 한 다음 즉시 '없음'으로 다시 설정한다는 것을 알 수 있습니다. 왜???

내가 제대로 질문을 undersand 경우에는 category-hed 이미 확장을 클릭하면, 그것은 최초의 "붕괴"때문에

<html> 
<head> 
<style type="text/css"> 
    .category  { width: 200px; } 
    .category-hed { cursor: pointer; } 
    .category-hed h2 { 
     background-image: url(/_img/b/category-hed-arrow-collapsed.gif); 
     background-repeat: no-repeat; 
     background-position: center right; 
    } 
    .category-hed h2.expanded { 
     background-image: url(/_img/b/category-hed-arrow-expanded.gif); 
    } 
    .category .stories { /* default hidden; clicking category-hed will toggle it */ 
     display: none; 
    } 
</style> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
<script type="text/javascript"> 
    $(function() { 
     // set click handlers on the category section headers to expand/collapse the sections 
     $(".category-hed").click(function() { 
      $(".category-hed h2").removeClass("expanded");  // unset the 'expanded' indicators in all categories 
      $(".stories").hide();        // hide the "stories" divs in all categories 

      // TODO: this doesn't collapse an already-expanded section... why? 
      $("h2", this).toggleClass("expanded"); // 2nd arg to $() sets context of the selection: h2 under this 
      $(this).siblings(".stories").toggle(); // show this category's "stories" div 
     }); 
     // the default story's category section should initially be expanded 
     var defaultStoryCatID = "#category-2"; 
     $("h2", defaultStoryCatID).toggleClass("expanded"); 
     $(".stories", defaultStoryCatID).show(); 
    }); 
</script> 

</head> 
<body> 
    <div class="category" id="category-1"> 
     <!-- nested H2 below to aid border styling; CSS omitted --> 
     <div class="category-hed"><h2>Category 1</h2></div> 
     <div class="stories"> 
      <div class="story"> 
       <h3>cat 1 story1 head</h3> 
       <p>... the story...</p> 
      </div> 
      <div class="story"> 
       <h3>cat 1 story2 head</h3> 
       <p>... another story...</p> 
      </div> 
     </div> 
    </div> 
    <div class="category" id="category-2"> 
     <div class="category-hed"><h2>Category 2</h2></div> 
     <div class="stories"> 
      <div class="story"> 
       <h3>cat 2 story1 head</h3> 
       <p>... the story...</p> 
      </div> 
      <div class="story"> 
       <h3>cat 2 story2 head</h3> 
       <p>... another story...</p> 
      </div> 
     </div> 
    </div> 
    <div class="category" id="category-3"> 
     <div class="category-hed"><h2>Category 3</h2></div> 
     <div class="stories"> 
      <div class="story"> 
       <h3>cat 3 story1 head</h3> 
       <p>... the story...</p> 
      </div> 
      <div class="story"> 
       <h3>cat 3 story2 head</h3> 
       <p>... another story...</p> 
      </div> 
     </div> 
    </div> 
</body> 
</html> 

답변

1

가장 먼저 할 일은 코드의 형식이 잘못되었습니다. 예 :

<div class="category" id="category-3"> 
    <div class="category-hed"><h2>Category 3</h2></div> 
     <div class="stories"> 
      <div class="story"> 
       <h3>cat 3 story1 head</h3> 
       <p>... the story...</p> 
      </div> 
      <div class="story"> 
       <h3>cat 3 story2 head</h3> 
       <p>... another story...</p> 
      </div> 
     </div> 
    </div> 
</div> 

에는 너무 많은/div 태그가 하나 있습니다. 그것은 ...

<div class="category" id="category-3"> 
    <div class="category-hed"><h2>Category 3</h2></div> 
    <div class="stories"> 
     <div class="story"> 
      <h3>cat 3 story1 head</h3> 
      <p>... the story...</p> 
     </div> 
     <div class="story"> 
      <h3>cat 3 story2 head</h3> 
      <p>... another story...</p> 
     </div> 
    </div> 
</div> 

내가 클릭 기능의 재 배열을 조금했다 ... 같이

$(".category-hed").click(function() { 
     // TODO: this doesn't collapse an already-expanded section... why? 
     $("h2", this).toggleClass("expanded"); // 2nd arg to $() sets context of the selection: h2 under this 
     $(this).siblings(".stories").toggle(); // show this category's "stories" div 

     $(".category-hed").not(this).find("h2").removeClass("expanded");  // unset the 'expanded' indicators in all categories 
     $(".category-hed").not(this).siblings(".stories").hide();    // hide the "stories" divs in all categories 
    }); 

그 밖으로 시도하고 내가 어떻게되는지 알려 주시기한다!

+0

Doh, 여분 div는 코드 - cleanup bug - 이야기 div가 카테고리 hed div (아이가 아닌)의 형제 *이기 때문에 그것을 보았어야만합니다. 조정 된 클릭 기능은 훌륭하게 작동합니다. - 감사합니다 !!! – Val

+0

듣기 좋게! -) – mikesir87

0

, 그것은합니다 (expanded 클래스를 제거하여)의 코드, 다음을 expanded 클래스는 즉시 .toggleClass()에 의해 다시 추가됩니다. 코드에서 이전 상태가 고려되지 않았기 때문에 항상 확장 될 수있는 무언가가 될 것입니다. 이 같은 간단한 문제 해결해야

$(".category-hed").click(function() { 
      if($(this).hasClass("expanded") 
       var dont_reexpand = true; 
      $(".category-hed h2").removeClass("expanded");  // unset the 'expanded' indicators in all categories 
      $(".stories").hide();        // hide the "stories" divs in all categories 
      if(!dont_reexpand) { 
       // TODO: this doesn't collapse an already-expanded section... why? 
       $("h2", this).toggleClass("expanded"); // 2nd arg to $() sets context of the selection: h2 under this 
       $(this).siblings(".stories").toggle(); // show this category's "stories" div 
      } 
     }); 

이 작업을 수행하는 아마 더 좋은 방법이 있습니다,하지만 당신은 아이디어를 얻을.

+0

이전 주를 고려하지 않는 것이 맞습니다. 하지만, 당신의 예제는 심지어 약간의 구문 probs (첫 번째 if 문에 닫힌 괄호가 누락 된; Mike는 상자에서 처리되었습니다.) 덕분에 나에게 도움이되지 않았습니다. 감사합니다, tho! – Val

관련 문제