简体   繁体   English

单击时隐藏下拉菜单-Bootstrap

[英]Hide dropdown menu when clicked - Bootstrap

I have a navigation bar in my website and all the links are loading a content in a container through AJAX. 我的网站上有一个导航栏,所有链接都通过AJAX将内容加载到容器中。

Now, I need to hide the dropdown menu when a link is clicked. 现在,我需要在单击链接时隐藏下拉菜单。 That's not the normal behavior of bootstrap. 这不是引导程序的正常行为。

Here's my code in the fiddle. 这是我的小提琴代码。

$(".dropdown-menu a").click(function() { $(this).closest(".dropdown-menu").prev().toggle(); });

http://jsfiddle.net/fw7vh/158/ http://jsfiddle.net/fw7vh/158/

I recently browsed into existing answers but it removes the dropdown button. 我最近浏览了现有答案,但删除了下拉按钮。 Please advise. 请指教。 Thanks! 谢谢!

Try with: 尝试:

$(".dropdown-menu a").click(function() {
    var menu = $(this).closest(".dropdown-menu");
    $(menu).css('display','none');
    setTimeout(function(){
        $(menu).css('display','');
    },200);
});

If I understood your question correctly, this might help you. 如果我正确理解了您的问题,这可能会对您有所帮助。

    $(".dropdown-menu a").click(function() {
    $(this).closest(".dropdown-menu").toggle();
});

You can achieve this easily with replacing your code 您可以通过替换代码轻松实现这一目标

$(".dropdown-menu a").click(function() {
  $(this).closest(".dropdown-menu").prev().toggle(); 
});

with this 有了这个

$(".dropdown-menu a").click(function() { 
   $(document.body).click(); 
});

Here is the working fiddle 这是工作的小提琴

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

相关问题 单击任何地方但菜单时,jQuery隐藏下拉列表 - jQuery hide dropdown when clicked anywhere but menu 单击页面上任何位置时如何隐藏下拉菜单 - How to hide dropdown menu when clicked anywhere on the page 单击下拉菜单按钮时,引导程序打开链接 - Bootstrap open link when clicked on dropdown menu button 当单击屏幕上的任意位置时,jQuery隐藏引导幻灯片菜单 - jQuery hide bootstrap slide menu when clicked anywhere on the screen 引导下拉菜单按钮在单击按钮以及单击屏幕时更改 - bootstrap dropdown menu button change when clicked on button and also when clicked on the screen 单击时引导程序下拉关闭 - Bootstrap dropdown closing when clicked Bootstrap 4:单击下拉项时如何关闭下拉菜单? - Bootstrap 4: How Do I Close a dropdown-menu when a dropdown-item is clicked? bootstrap下拉按钮:如何在单击页面的其他部分时隐藏下拉菜单? - bootstrap dropdown button: How to hide dropdown menu when click other part of page? 单击特定列表项时,在引导程序下拉列表上取消隐藏事件 - Cancel hide event on bootstrap dropdown when a specific list item has been clicked 在输入字段中单击时,菜单下拉菜单中的表单不会保留-Bootstrap3 - Form inside a menu-dropdown does not stay when clicked in the input field - Bootstrap3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM