2011-10-06 3 views
2

그래서 .toggle()을 사용하여 여러 div를 열고 닫았지만 첫 번째 div는 페이지로드시 열었습니다. 문제는 사용자가 해당 div를 클릭하여 닫을 때 두 번 클릭하면 첫 번째 클릭이 다시 열리도록 시도하는 것입니다. 어떻게해야합니까?Jquery .toggle() first element open

$(document).ready(function(){ 
    $('.content_accrd').css({"height":"0px"}); 
    $('.content_accrd:eq(0)').css({"height":"100px"}); 
    $('.paneltop:eq(0)').css({"background-position":"0px -21px"}) 
$('.paneltop').toggle(function() { 
    $(this).css({"background-position":"0px -21px"}) 
    .siblings('.content_accrd').animate({"height":"100px"}, 200) 
}, function() { 
$(this).css({"background-position":"0px 0px"}) 
    .siblings('.content_accrd').animate({"height":"0px"}, 200); 

}) 
}) 

태그 :

<div id="panel1" class="panel"> 
       <h2 class="panel_title">Capad Clients</h2> 
       <div class="paneltop"> 
       </div> 
       <div class="content_accrd"> 
       <p> 
text 
       </p> 
       </div> 
      </div> 

      <div id="panel1" class="panel"> 
       <div class="paneltop"> 
       </div> 
       <div class="content_accrd"> 
       <p> 
       text 
       </p> 
       </div> 
      </div> 


      <div id="panel1" class="panel"> 
       <div class="paneltop"> 
       </div> 
       <div class="content_accrd"> 
       <p> 
text 
       </p> 
       </div> 
      </div> 

답변

1

Example

편집 :

이 코드
$(document).ready(function() { 
    $('.content_accrd').css({ 
     "height": "0px" 
    }); 
    $('.content_accrd:eq(0)').css({ 
     "height": "100px" 
    }); 
    $('.paneltop:eq(0)').css({ 
     "background-position": "0px -21px" 
    }) 
    $('.paneltop').click(function() { 
     if ($(this).siblings('.content_accrd').height() != 100) { 
      $(this).css({ 
       "background-position": "0px -21px" 
      }).siblings('.content_accrd').animate({ 
       "height": "100px" 
      }, 200) 
     } else { 
      $(this).css({ 
       "background-position": "0px 0px" 
      }).siblings('.content_accrd').animate({ 
       "height": "0px" 
      }, 200); 
     } 
    }) 
}) 
+0

, 문제는 지금, 다른 div의에 처음 공개 사업부에서 마이그레이션 것 같습니다 활성화하려면 두 번 클릭해야합니다. –

+0

마크를 게시 할 수 있습니까? – Joe

+0

토글이 여러 클래스에 너무 광범위하기 때문에 형제 높이를 기준으로 체크하고 토글 한 편집 – Joe