2013-12-22 4 views
2

내 탐색이 페이지를로드하지 않고로드되기를 원합니다. 모두 작동합니다. 그러나 내비게이션 요소 <li> nav </li>은 다른 스타일을 가진 클래스를 가져야합니다. "활성"입니다. 나는이 시도했지만 작동하지 않습니다, 당신 CANN 내 page 코드에 무슨 일이 일어 나는지 :JQuery 탐색 세트 클래스

  $('#content').children('section').not('#home').hide(); 
      $(".a").hasClass('navigation-element').parent().removeClass('active'); 
      $(".a").click(function(e){ 
       e.preventDefault(); 

       if ($(this).hasClass('navigation-element')){ 
        var $target = $('#' + $(this).attr('href')).stop(true, true); 
        var $secs = $('#content > section').not($target).stop(true, true).filter(':visible'); 
        if ($secs.length) { 
         $secs.fadeOut(function() { 
          $target.fadeIn(); 
          $(this).parent().addClass('active'); 
         }); 
        } else { 
         $target.fadeIn(); 
        } 
       } else if ($(this).hasClass('hide')){ 
        $(this).parent().fadeOut(); 
       } 
      }); 

오류는 라인 2에 있고 12

답변

1

행 2 : hasClass 부울 값을 반환 여기 this 클릭 한 li 요소를 참조하지 않습니다, 당신은 폐쇄 변수가 클릭 된 요소

//initial setup 
$('#content').children('section').not('#home').hide(); 
$('.active:has(a.anavigation-element)').removeClass('active'); 
$('.navigation-element[href="home"]').parent().addClass('active'); 

$('.navigation-element').click(function (e) { 
    var $this = $(this); 
    e.preventDefault(); 

    var $target = $('#' + $(this).attr('href')).stop(true, true); 
    var $secs = $('#content > section').not($target).stop(true, true).filter(':visible'); 

    if ($secs.length) { 
     //remove existing active from the prev element 
     $('.active:has(a.navigation-element)').removeClass('active'); 
     $secs.fadeOut(function() { 
      $target.fadeIn(); 
      $this.parent().addClass('active'); 
     }); 
    } else { 
     $target.fadeIn(); 
     $this.parent().addClass('active'); 
    } 
}); 
를 참조 사용할 필요가 : 당신은 클래스 필터

라인 (12)을 사용할 필요가