2014-04-18 3 views
0

간단한 드롭 다운 스크립트가 있는데 메뉴 바깥 쪽을 클릭하여 열려있는 모든 드롭 다운을 숨기고 싶습니다. 그러나 작동하지 않는 것 같습니다. 왜 그 이유를 알고 있습니까?메뉴 바깥 쪽을 클릭 할 때 숨기는 드롭 다운

여기에서 찾을 수 있습니다 : http://codepen.io/dr-potato/pen/rLleC?editors=101

HTML

<ul> 
    <li><a href="#">Home</a></li> 
    <li class="Navigation-listItem is-dropdown"> 
    <a href="#">About</a> 
    <ul class="Navigation-list is-dropdown is-hidden"> 
     <li>Johnny</li> 
     <li>Julie</li> 
     <li>Jamie</li> 
    </ul> 
    </li> 
    <li class="Navigation-listItem is-dropdown"> 
    <a href="#">Contact</a> 
    <ul class="Navigation-list is-dropdown is-hidden"> 
     <li>Johnny</li> 
     <li>Julie</li> 
     <li>Jamie</li> 
    </ul> 
    </li> 
</ul> 

당신은 당신의 드롭 다운의 표시 속성을 변경할 수 있습니다

$(document).ready(function() { 
    $('.Navigation-listItem').click(function() { 
     $(this).children('.Navigation-list.is-dropdown').toggleClass('is-hidden'); 
    }); 
}); 

/* Anything that gets to the document 
    will hide the dropdown */ 
$(document).click(function(){ 
    $(".Navigation-listItem.is-dropdown").addClass('is-hidden'); 
}); 

/* Clicks within the dropdown won't make 
    it past the dropdown itself */ 
$(".Navigation-listItem.is-dropdown").click(function(e){ 
    e.stopPropagation(); 
}); 
+1

드롭 다운이 나타나지 않는 것 같습니다. – 4dgaurav

+0

@ Gaurav : 죄송합니다. JS –

+0

@ Gaurav에 맞춤법 오류가 있지만 문제는 여전히 지속됩니다. –

답변

1

Working Fiddle

jQuery를 코드

$(document).ready(function() { 
    $('.Navigation-listItem').click(function() { 
     $(this).children('.Navigation-list.is-dropdown').toggleClass('is-hidden'); 
    }); 


    /* Anything that gets to the document 
    will hide the dropdown */ 
    $(document).on('click', function (event) { 
     if ($(event.target).closest('#menu').length == false) { 
      $(".Navigation-list.is-dropdown").addClass('is-hidden'); 
     } 
    }); 

    /* Clicks within the dropdown won't make 
    it past the dropdown itself */ 
    $(".Navigation-listItem.is-dropdown ").click(function (e) { 
     e.stopPropagation(); 
    }); 
}); 

도와주세요 this answer

+0

열려있는 메뉴를 아는 동안 '클릭'이벤트 만 활성화하려면이 항목을 확장 할 수 있습니다. 그렇지 않으면 모든 클릭이이 처리기를 거쳐 간다. 아마도 무시할 만하다는 것이 이상적이지 않다. –

+0

왜 지구상에서 의도적으로 'length == false'를 할 것인가? –

+0

@Simon_Weaver [answer] (http://stackoverflow.com/) a/10851375/2260614) 왜'length == false'를 사용했는지에 대해 설명합니다. 'open'_ 될 메뉴를 알고있는 동안 'click'이벤트를 '___enable'하는 방법을 잘 모르겠습니다. 어떤 도움이 .. !!? – Bhavik

0

CSS

.Navigation-list { 
    display: block; 
} 

.Navigation-list.is-hidden { 
    display: none; 
} 

JS 이 방법. 이것은 단지 대략적인 코드 일뿐입니다.

     if(dropDownShow.css('display') != 'block'){ 
          dropDownShow.css('display', 'block'); 
          dropDownShow.css('position', 'absolute'); 

         } 
         else{ 
          dropDownShow.css('display', 'none'); 
         } 
0
당신이 준 정보와 나는 그것을 작동 볼 수없는 codepen으로

,하지만 난 가정이 $ (문서) .click (함수() 드롭 아래로 내부에 있기 때문에 작동하지 않습니다 숨길 수 당신이 그것을 클릭하면 문서 그래서, 내가 당신이 게시물 How to hide/show drop down list content in HTML를 참조하는 것이 좋습니다. 사라질 것입니다.

+0

JS에서 맞춤법 오류가 있었지만 수정했습니다. –

+0

나는 그것을 듣게되어 기쁩니다. – qaztype

+0

글쎄 그건 내 질문을 해결하지 못했습니다, 그래서 어떤 도움을 아직도 부탁드립니다 :). –

관련 문제