2013-02-19 4 views
0

사용자가 열린 상태를 벗어날 때 내 드롭 다운 메뉴를 숨기려고합니다. Im은 메뉴가 열려 있는지 여부를 결정하는 플래그 isActive을 사용합니다. 클릭 이벤트가 추가되면 문서를 클릭하여 메뉴를 숨기고 메뉴가 클릭되면 전파를 중지합니다. 지금은 드롭 다운 앵커 태그를 클릭하면 문서 클릭 이벤트가 발생합니다. 아무도 내가 이것을 고치는 방법을 조언 할 수 있습니까?드롭 다운 메뉴가 열려 있고 사용자가 문서를 클릭 할 때 드롭 다운 메뉴를 닫으려고 시도합니다.

$(document).on('click', function(e){ 
    if(isActive && $(e.target).closest('.social-share-options').length === 0){ 
     $('.social-share-options').removeClass('is-active'); 
     isActive = false; 
    } 
}); 
+0

당신은 나에게 어쩌면 다른 요소가 클릭을 받고 있음을 생각하게하는 [바이올린] (http://jsfiddle.net/) – Anujith

+0

귀하의 코드가 작동해야을 게시 당신이 콘솔을하고 시도 할 수 있습니다. .social-share-options에 대한 클릭 이벤트 처리기에 로그인하십시오. 이것은 .social-share-options가 실제로 click 이벤트를 수신 하는지를 알려줍니다. – Hendeca

답변

1

클릭 한 어떤 경우 검사 드롭 다운 여부를 내이며, 클릭 된 어떤 경우 다음 드롭 다운을 숨길 조건을 확인 드롭 다운 내에 있지 테스트하지는 않았지만 e.stopPropagation()을 추가해야 이벤트가 버블 링되지 않습니다.

$('.btn-social-share').on('click', function(e){ 
     e.preventDefault(); 
     e.stopPropagation(); 
     //... 

http://api.jquery.com/event.stopPropagation/

+0

이것은 하나의 솔루션이지만 반드시 원래의 문제에 대한 해결책을 제공하지는 않습니다. 코드가 제대로 작동해야하지만 아마 클릭을받는 다른 요소가있을 것입니다. 이는 어느 것이 든 완전히 가능한 솔루션이라고 말했습니다. – Hendeca

1

:

JS

//User profile share tooltip 
     $('.btn-social-share').on('click', function(e){ 
      e.preventDefault(); 

       if(!isActive){ 
        $('.social-share-options').show(); 
        isActive = true; 
       } else { 
        $('.social-share-options').hide(); 
        isActive = false; 
       } 

     }); 

     /* Anything that gets to the document 
      will hide the dropdown */ 
     $(document).on('click', function(){ 
      if(isActive){ 
       $('.social-share-options').hide(); 
       isActive = false; 
      } 
     }); 

     /* Clicks within the dropdown won't make 
      it past the dropdown itself */ 
     $('.social-share-options').click(function(e){ 
      e.stopPropagation(); 
     }); 
관련 문제