简体   繁体   English

单击某个选项后,如何使下拉菜单消失?

[英]How can you make a drop down menu disappear after you click on an option?

I have just a small dropdown menu and I would like to be able to make it so the drop down menu disappears when I click on one of the options (not the button, but just the menu you get when you hover over the button). 我只有一个小的下拉菜单,我希望能够做到这一点,当我点击其中一个选项(不是按钮,而只是你将鼠标悬停在按钮上时获得的菜单)时,下拉菜单消失了。 I've been looking around for solutions to this, but I can't seem to find any answers. 我一直在寻找解决方案,但我似乎无法找到任何答案。

 function changeType(str) { document.getElementById("selectType").innerHTML = str; document.getElementById("selectType").value = str; } 
 .dropbtn { background-color: #360363; margin-bottom: 0px; min-width: 120px; color: white; padding: 0px; font-size: 0.7em; border: none; cursor: pointer; } .dropdown { position: relative; display: inline-block; margin-top: 0px; } .dropdown-content { display: none; position: absolute; background-color: #f9f9f9; margin-top: 0px; min-width: 120px; box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); z-index: 1; } .dropdown-content a { color: black; padding: 6px 16px; text-decoration: none; display: block; margin-top: 0px; } .dropdown-content a:hover { background-color: #f1f1f1 } .dropdown:hover .dropdown-content { display: block; } .dropdown:hover .dropbtn { background-color: #4B0082; transition: 0.5s; } 
 <div class="dropdown"> <button id="selectType" class="dropbtn" value="Choose Color">Choose Color</button> <div class="dropdown-content"> <a onclick="changeType('Red')" href="#">Red</a> <a onclick="changeType('Green')" href="#">Green</a> </div> </div> 

You can achieve this via CSS: 你可以通过CSS实现这个目标:

.dropdown:active .dropdown-content,
.dropdown:focus .dropdown-content {
    display: none;
}

Snippet: 片段:

 function changeType(str) { document.getElementById("selectType").innerHTML = str; document.getElementById("selectType").value = str; } 
 .dropbtn { background-color: #360363; margin-bottom: 0px; min-width: 120px; color: white; padding: 0px; font-size: 0.7em; border: none; cursor: pointer; } .dropdown { position: relative; display: inline-block; margin-top: 0px; } .dropdown-content { display: none; position: absolute; background-color: #f9f9f9; margin-top: 0px; min-width: 120px; box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); z-index: 1; } .dropdown-content a { color: black; padding: 6px 16px; text-decoration: none; display: block; margin-top: 0px; } .dropdown-content a:hover { background-color: #f1f1f1 } .dropdown:hover .dropdown-content { display: block; } .dropdown:hover .dropbtn { background-color: #4B0082; transition: 0.5s; } .dropdown:active .dropdown-content, .dropdown:focus .dropdown-content { display: none; } 
 <div class="dropdown"> <button id="selectType" class="dropbtn" value="Choose Color">Choose Color</button> <div class="dropdown-content"> <a onclick="changeType('Red')" href="#">Red</a> <a onclick="changeType('Green')" href="#">Green</a> </div> 

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

相关问题 下拉菜单,当您单击一个选项时,将下载一个文件 - Drop-down menu that when you click on an option a file will be downloaded <a>点击链接后</a>如何在javascript中使<a>元素消失?</a> - How to make in javascript that <a> element disappear after you click on link? 如何制作月份、日期和年份的下拉列表? - How can you make drop down lists for months, dates, and years? 打开下拉菜单并单击 React 中的特定类别后,如何使下拉菜单消失 - How to make a dropdown menu disappear once you open it and click on a specific category in React 单击后是否可以水平扩展下拉菜单? - Is there a way to expand a drop down horizontally after you click? 从SuiteScript的下拉菜单中选择选项后,如何显示文本框? - How do I make a text box appear after selecting an option from a drop-down menu in SuiteScript? 回发后单击下拉列表按钮时,使用Jquery菜单列表的下拉列表消失 - Dropdown list using Jquery menu list disappear when click on drop down list button after postback 下拉菜单:如何取消激活第一个(选定)选项? - drop down menu: how to make deactivate the 1st (selected) option? 如何禁用下拉菜单中的选项 - How do you disable options in a drop down menu 您如何更改下拉菜单以使用触摸屏? - How do you change a drop down menu to work with touch screens?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM