2011-08-02 6 views

답변

1

이 기능을 바인딩 할 필요가 없습니다 사용할 수 있습니다.

은 어느 당신은 볼 또는 숨겨진 경우 표시되는 경우 섹션을 숨 깁니다 toggleSection (라는 함수에 클릭 이벤트)를 결합, 또는 그냥 사용

$('.section.hidden').click(function() { 
    $(this).toggle(); 
}); 
0

당신은

$('.section-legend, .section-collapse').click(hidesection); 

function hideSection() { 
    $(this).addClass('hidden'); 
} 
0

jQuery를 어떻게 달성 할 수있는 쉬운 방법을 제공을 당신은하고 싶지 :

.toggleClass()

당신이 손으로 변화를 구현해야하지만, 오히려 작동하지 않습니다 그런 식으로 한번의 클릭 결합

그냥 객체를 숨기고 표시 할 것 과정의 경우에는 손으로 코딩하는 대신

.toggle()

를 사용할 수 있습니다. 이것은 주로 Javascript-Frameworks가 당신을 위해하는 일이며, 직접 구현하는 대신 기존 기능을 구현하여 탭할 수 있습니다. 또한 브라우저 간 비 호환성으로 인해 (대부분) 처리됩니다.

jQuery를 최대한 활용하려면 The jQuery API을 파헤 치면서 제공하는 모든 기능을 알아보십시오. 그렇게하면 많은 시간을 절약 할 수 있습니다. 그렇지 않으면 이미 존재하는 기능을 개발할 수 있습니다.

0

필요한 것이 모두 가시성이면 토글 만 사용하십시오. 섹션을 숨기기위한 사용자 정의 CSS가 있으면 toggleClass를 사용하십시오. 설명하려면

<script type="text/javascript"> 
    $(function() { 

     $('p').click(function() { 
      $('#book').toggle(); 



      // In javascript, just like many C-derived languages, you can pass 
      // the method's pointer to a variable in. 
      // hidden is the custom class you add and remove from a section 
      f = $('#great').is('.hidden') ? funky : hacky; 

      f($(this)); 



      $('#great').toggleClass('hidden'); 

      $('#amazing').toggleClass('man'); 

      alert($('#great').attr('class') + ' ---- ' + $('#amazing').attr('class')); 
     }); 


     function funky(section) { 
      $(section).html('this is funky!'); 
     } 

     function hacky(section) { 
      $(section).html('this is hacky!'); 
     } 

    }); 
</script> 

<div id='book' style='display: none'>Hello</div> 

<div id='great' class='diz hidden whatever'>Hai</div> 
<div id='amazing' class='spider'>Hai</div> 

<p>Stackoverflow rules!</p> 

섹션을 숨기기 시작하려면 style=display: none을 태그에 입력하십시오. 사용자 정의 숨기기/형식 지정의 경우 toggleClass를 사용하십시오.

관련 문제