2014-01-11 7 views
0

3 단계 탐색 기능이 있습니다. 링크 중 하나를 클릭하면 레벨에 따라 클래스 이름을 얻습니다. 레벨 1은 클래스 이름 "active1", 레벨 2 "active2", 레벨 3 "active3"을 갖습니다.jQuery : 동일 레벨 삭제 클래스

클릭하면 클래스 이름을 가져올 수 있지만 다른 클래스 이름은 클릭하면 바로 삭제됩니다.

플랜은 첫 번째 레벨의 링크를 클릭하면 두 번째 레벨이 표시되고 두 번째 레벨의 링크를 클릭하면 세 번째 레벨이 표시됩니다.

이 문제를 어떻게 해결할 수 있습니까?

HTML :

<div id="nav"> 
<ul class="first"> 
    <li><a href="#" title="Nav" class="active">Link</a> 
     <ul class="second"> 
      <li><a href="#" title="Sub Nav">Link 2</a> 
       <ul class="third"> 
        <li><a href="#" title="Sub Nav 2">Link 3</a></li> 
        <li><a href="#" title="Sub Nav 2">Link 3</a></li> 
       </ul> 
      </li> 
      <li><a href="#" title="Sub Nav">Link 2</a></li> 
      <li><a href="#" title="Sub Nav">Link 2</a></li> 
     </ul> 
    </li> 
    <li><a href="#" title="Nav">Link</a></li> 
    <li><a href="#" title="Nav">Link</a></li> 
    <li><a href="#" title="Nav">Link</a></li> 
</ul></div> 

JS/jQuery를

DEMO처럼
$('.first a').click(function() { 
    if ($(this).hasClass('active')) { 
     return false; 
    } else if ($(this).not('active')) { 
     $('.first').find('.active').removeClass('active'); 
     $(this).addClass('active'); 
    } 
    return false; 
}); 

$('.second a').click(function() { 
    if ($(this).hasClass('active2')) { 
     return false; 
    } else if ($(this).not('active2')) { 
     $('.second').find('.active2').removeClass('active2'); 
     $(this).addClass('active2'); 
    } 
    return false; 
}); 

$('.third a').click(function() { 
    if ($(this).hasClass('active3')) { 
     return false; 
    } else if ($(this).not('active3')) { 
     $('.second').find('.active3').removeClass('active3'); 
     $(this).addClass('active3'); 
    } 
    return false; 
}); 
+0

정말 세 가지 다른 활성 수업이 필요합니까? 스타일 지정이라면'#nav> ul> li a.active'와 같은 자식 선택자'>'와 차별화 할 수 있습니다.'.first','.second'도 필요 없습니다. –

+0

@ user3095496 : 실제로 나는 더 나은 해결책을 얻었습니다. 그냥 내 대답을 업데이 트하고 퇴색 대신 토글로 데모. 다시 클릭하면 사라집니다. –

+0

@koala_dev 아이디어에 감사드립니다 !! – marcandrew

답변

1

: JS

$('#nav').on('click', 'a', function(){ 
var nextul = ($(this).closest('li').children('ul')); 
nextul.toggle('slow'); 
}); 

CSS

.second, .third {display:none;} 
.active{display:block;} 
+0

예! 고맙습니다! 그게 훨씬 나아! 활성 링크를 닫아야하는 첫 번째 레벨의 다른 링크 중 하나를 클릭하면 기능을 구축 할 수 있습니까? – marcandrew