admin管理员组文章数量:1430432
I have created a dropdown menu using bootstrap with the following code:
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Examples </a><ul class="dropdown-menu" id="examples">
<li><a href="#" class="example" id="atlas" data-example-name="atlas">Atlas</a> </li>
<li><a href="#" class="example" id="map" data-example-name="map"> Map</a></li>
</ul>
</li>
However, I am having trouble hiding the dropdown menu after a user clicks on one of the menu items. I have tried using Jquery, giving the following code:
$("#atlas").click(function(){
$("#examples").show();
})
but it requires me to click on a menu item twice (first time to perform its desired action, second time to finally hide it).
I have created a dropdown menu using bootstrap with the following code:
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Examples </a><ul class="dropdown-menu" id="examples">
<li><a href="#" class="example" id="atlas" data-example-name="atlas">Atlas</a> </li>
<li><a href="#" class="example" id="map" data-example-name="map"> Map</a></li>
</ul>
</li>
However, I am having trouble hiding the dropdown menu after a user clicks on one of the menu items. I have tried using Jquery, giving the following code:
$("#atlas").click(function(){
$("#examples").show();
})
but it requires me to click on a menu item twice (first time to perform its desired action, second time to finally hide it).
Share Improve this question edited May 11, 2016 at 14:45 koninos 5,3775 gold badges33 silver badges50 bronze badges asked May 11, 2016 at 14:39 ke.like.li 1372 gold badges3 silver badges12 bronze badges 2- I suggest you to add a "toggled" class that will show the menu, you can also add CSS transitions on it.. (sorry I don't have time to build an example) The jQuery will just have a ToggleClass method that will add or remove – miguelmpn Commented May 11, 2016 at 14:43
- are you trying to hide the entire drop down menu, or just the list items? – devlin carnate Commented May 11, 2016 at 14:48
4 Answers
Reset to default 1There's a lot that could be done to improve this but mainly you just need to call hide on the menu when an element is clicked. Here is a codepen of it working.
// show/hide the menu when examples is clicked
$(".dropdown-toggle").on("click", function () {
$(".dropdown-menu").toggle();
});
// hide the menu when an exmple is clicked
$(".example").on("click", function(){
$(".dropdown-menu").hide();
});
I would say
$('.example').click(function(){ $('#examples').hide(); });
I think you want to this code.
dropdown-menu show always items. you click an item, hide it.
$('.example').on('click', function (event) {
event.stopPropagation();
$(this).hide();
});
Here's a fiddle
You can bind your event to the li itself, and hide it when clicked:
$('ul li').click(function() {
$(this).hide();
});
Here is a Fiddle Demo.
If you want to use a class, you can do:
$('.example').click(function() {
$(this).hide();
});
Here is a Fiddle Demo using the class.
本文标签: javascriptHow to hide a drop down menu after user clicks on an itemStack Overflow
版权声明:本文标题:javascript - How to hide a drop down menu after user clicks on an item? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745515304a2661501.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论