2012-07-09 7 views
0

div가 숨겨져 있는지 확인하기위한 올바른 jQuery 구문 & &에 "보기"클래스가 있습니까? 그렇다면 .3 초 후에 클래스 "보기"로 다음 숨겨진 div를 표시 하시겠습니까?if 문이 jQuery에서

참고 현재 작동하지 않지만 클래스에 관계없이 모든 숨겨진 div가 사라집니다.

<script type="text/javascript"> 
      $(function() { 
       // Start showing the divs 
       showDiv(); 
      }); 

      function showDiv() { 
       // If there are hidden divs left 
       if($('div:hidden').length.hasClass('view')) { 
        // Fade the first of them in 
        $('div:hidden:first').fadeIn(); 
        // And wait one second before fading in the next one 
        setTimeout(showDiv, 300); 
       } 
      } 
    </script> 
+3

.length.hasClass() 란 무엇입니까? –

+0

jQuery 선택기와 관련하여 * Javascript * – Radu

+0

의 비어있는 div가 비어 있지 않고 클래스가 'view'인 if와는 관련이 있습니까? 그런 조건을 결합 할 수 있습니까? && 대신 사용해야합니까? – kikix2125

답변

4

은 아마 더 나은 솔루션

if($('div.view:hidden').length) { 
    // Fade the first of them in 
    $('div.view:hidden:first').fadeIn(); 
    // And wait one second before fading in the next one 
    setTimeout(showDiv, 300); 
} 
+1

'if ($ ('div.view : hidden '). 길이)'? –

+0

@PhilippeLeybaert 옙, 내 사본과 붙여 넣기가 실패했습니다. –

+2

''div div = $ ('div.view : hidden')',''divs.first()'] (http://api.jquery.com/first) /) – bdukes

2
// get all hidden divs with a class of view 
var $hiddenViewDivs = $('div.view:hidden'); 
// if there are any... 
if ($hiddenViewDivs.length) { 
    // get the first one and invoke fadeIn() on it 
    $hiddenViewDivs.first().fadeIn(); 
    // And wait one second before fading in the next one 
    setTimeout(showDiv, 300); 
} 
입니다

난 당신의 코드가 (그 별 주위에 내 새로운 의견을 참조) 무엇을하고 있었는지 설명 할 것 :

// **Get the length (count) of all hidden divs and invoke hasClass('view') on that number (will throw an error)** 
if($('div:hidden').length.hasClass('view')) { 
    // **Get the first hidden div and invoke fadeIn() on it, regardless of if it has a class of view or not** 
    $('div:hidden:first').fadeIn(); 
    // And wait one second before fading in the next one 
    setTimeout(showDiv, 300); 
} 

을 편집 (대체 솔루션) :

이 솔루션의 장점은 DOM에서 $('div.view:hidden')을 계속 찾을 필요가 없다는 것입니다. 여기서, 당신은 그것을 한 번 얻고 공연 신을 달래줍니다.

function showDiv() { 
    var $hiddenViewDivs = $('div.view:hidden'); 
    var i = 1; 
    $hiddenViewDivs.each(function(){ 
     (function($target){ 
      setTimeout(function(){$target.fadeIn();}, 300*i); 
     })($(this)); 
     i++; 
    }); 
}​ 

예 : http://jsfiddle.net/lbstr/LdzYm/

+0

아주 멋지다! 감사! – kikix2125

0

어떻게 바로 $('div.view:hidden').length 어떻습니까?

0
$('div.view:hidden:first').fadeIn(function() { 
    setTimeout(showDiv, 300); 
}); 

if는 필요하지 않습니다. jQuery가이를 처리합니다.