简体   繁体   English

切换下拉菜单并在外部单击隐藏

[英]Toggle dropdown and hide on outside click

I got this dropdown menu which works, but I would also like it to close when I click outside it… I've tried some answered solutions but something's wrong and I can't figure it out..Could someone point what am I missing out here? 我得到了可以使用的下拉菜单,但是当我在菜单外部单击时,我也希望它关闭...我尝试了一些答案的解决方案,但是出了点问题,我无法弄清楚。.有人可以指出我错过了什么这里? Thanks a lot 非常感谢

 $("#toggle").on('click', function() { $(".dropdown").toggleClass("dropdown--visible"); }); $(document).click(function(){ $(".dropdown").hide(); }); $(".dropdown").click(function(e){ e.stopPropagation(); }); 
 .dropdown { background: red; display: none; } .dropdown--visible { display: block; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="toggle">Toggle dropdown</button> <div class="dropdown"> <ul> <li>one</li> <li>two</li> </ul> </div> 

 $("#toggle").on('blur click', function() { $(".dropdown").toggleClass("dropdown--visible"); }); $(document).click(function(){ }); 
 .dropdown { background: red; display: none; } .dropdown--visible { display: block!important; } .dropdown--invisible { display: none; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="toggle">Toggle dropdown</button> <div class="dropdown"> <ul> <li>one</li> <li>two</li> </ul> </div> 

You have setup a click event listener for document and it will execute all the time when you click anywhere. 您已经为文档设置了单击事件侦听器,当您单击任意位置时,它将一直执行。 You can use a common class to filter out the dropdown events. 您可以使用通用类过滤掉下拉事件。

<button id="toggle" class="dd">Toggle dropdown</button>

<div class="dropdown dd">
  <ul>
    <li>one</li>
    <li>two</li>
  </ul>
</div>

$(document).click(function(e){
  if (!$(e.target).hasClass('dd')) {        
    $(".dropdown").removeClass("dropdown--visible");
  }
});

Also better use removeClass() without using hide() since hide() adds a display:none; 也最好不使用hide ()而使用removeClass() ,因为hide()添加了display:none; directly to the element and will be hard to control. 直接到元素,将很难控制。

This vanilla-flavored solution uses a handleDropdown function that checks two conditions: 此香草味解决方案使用handleDropdown函数来检查两个条件:
- Was the toggle button clicked? -是否点击了切换按钮?
- Is the dropdown currently hidden? -下拉菜单当前隐藏吗?

If both are true, it shows the dropdown. 如果两者都为真,则显示下拉列表。 Otherwise, it hides the dropdown. 否则,它将隐藏下拉菜单。

 const dropdown = document.getElementsByClassName("dropdown")[0], toggle = document.getElementById("toggle"); document.addEventListener("click", handleDropdown); function handleDropdown(event){ if(event.target == toggle && dropdown.style.display != "block"){ dropdown.style.display = "block"; } else{ dropdown.style.display = "none"; } } 
 .dropdown { background: red; display: none; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="toggle">Toggle dropdown</button> <div class="dropdown"> <ul> <li>one</li> <li>two</li> </ul> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM