twitter bootstrap dropdown doesn't toggle closed when it should

后端 未结 8 1033
伪装坚强ぢ
伪装坚强ぢ 2021-02-10 00:16

Oh man, I\'ve been tearing my hair out over this. 4 hours on a dropdown.

I\'m using Twitter Bootstrap.

The fixed nav at the top has a dropdown, pretty standard s

相关标签:
8条回答
  • 2021-02-10 00:48

    Nothing worked for me, except this:

    $('.dropdown-menu li a').on('click', function(e) {
        $('.dropdown-toggle').dropdown('toggle');
        e.stopPropagation();
    });
    
    0 讨论(0)
  • 2021-02-10 00:49

    My fix for more than one dropdowns in nav menu: Other solutions doesn't work for me

    $('.dropdown-toggle').on('click', function (e) {
          $('.dropdown-toggle').each(function (i,elem) {
              if (elem != e.target) $(elem.parentElement).removeClass('open');
          });
    })
    
    0 讨论(0)
  • 2021-02-10 00:56

    iframes don't propagate click events up to their parents by default. You need to add code to do that manually. In addition to being able to edit the content of the iframe you are loading, cross-domain restrictions will require you to be loading the iframe content from the same domain as your page.

    This would need to run from within the iframe page:

    $('html').on('click', function () {
      parent.$('#frame').trigger('click');
    });
    

    where #frame is the id of your iframe. You could delegate the click to anything in the parent (e.g., 'body') and that would work as well. Depends on how tightly you want to couple the content in the iframe to the page.

    So, that should take care of the page closing the menu.

    Important Update

    As for clicking on the menu items to close the menu, apparently there was a bug[?] in the lastest update (2.0.4 > 2.1.0): Dropdown menus don't get closed when selecting an option. A selector was changed from '.dropdown form' to '.dropdown', so now what was once a celebrated feature to make working with dropdowns in forms easier has evolved into a callback that blocks all clicks on dropdown menu items.

    An interim solution is to include the following code until the bug is fixed (2.1.1):

    $('body')
      .off('click.dropdown touchstart.dropdown.data-api', '.dropdown')
      .on('click.dropdown touchstart.dropdown.data-api'
        , '.dropdown form'
        , function (e) { e.stopPropagation() })
    
    0 讨论(0)
  • 2021-02-10 00:57

    you can remove the "open" class from dropdown menus when another is clicked. this worked for me so far:

    $(document).ready(function () {
        $('a.dropdown-toggle').each(function(){
            $(this).on("click", function () {
                $('li.dropdown').each(function () {
                    $(this).removeClass('open');
                });
            });
        });
    });
    
    0 讨论(0)
  • 2021-02-10 00:58

    I was seeing the same problem with 2.1.1.

    The solution that was posted on GitHub which worked easily for me was:

    $('body').on('touchstart.dropdown', '.dropdown-menu', function (e) { e.stopPropagation(); });

    Credit to blakeembrey at https://github.com/twitter/bootstrap/issues/5094

    0 讨论(0)
  • 2021-02-10 01:06

    The bug is fixed now, You can use https://github.com/twitter/bootstrap/blob/2.1.1-wip/js/bootstrap-dropdown.js , the 2.1.1-wip for dropdown

    0 讨论(0)
提交回复
热议问题