【问题标题】:Prevent bootstrap dropdown closing when clicked anywhere else other than the menu item itself当单击菜单项本身以外的任何其他位置时,防止引导下拉菜单关闭
【发布时间】:2017-07-12 10:47:30
【问题描述】:

我需要在子菜单处于活动状态时打开子菜单(就像用户在该页面上时一样)。已经这样做了:

// To open active submenu on load
jQuery('.keep-open').find('.current-menu-item').closest('.current-menu-parent').children('.dropdown-toggle').trigger('click');

但单击任意位置时,菜单会关闭。它需要保持打开状态,除非它被特别点击。仅在使用此方法在外部单击时才设法防止它关闭:

// To prevent submenu from being closed on outside click
jQuery('.current-menu-parent').on('hide.bs.dropdown', function() {
  return false;
});

当然,即使单击菜单本身,这似乎也阻止了它关闭。如何指定它仅在单击该菜单项时关闭?

我找到的最接近的参考是 thisthis,但到目前为止仍然无法掌握如何去做。这是通过 wp_bootstrap_navwalker 在 WordPress 网站菜单上的。

Jsfiddle here

【问题讨论】:

    标签: javascript jquery twitter-bootstrap


    【解决方案1】:

    如果您只想在单击该菜单时关闭,只需添加此代码

    // Hide dropdown when click menu
    jQuery('.current-menu-parent').bind('click', function() {
            $(this).find('ul').fadeOut();
    });
    

    Demo here

    或者您想在单击该菜单时再次显示菜单项:

    // toggle dropdown when click menu
    jQuery('.current-menu-parent').bind('click', function() {
            $(this).find('ul').fadeToggle();
    });
    

    Demo here

    【讨论】:

    • 谢谢!改用 .toggle() 以保留原始行为。
    猜你喜欢
    • 2018-02-16
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多