twitter bootstrap dropdown doesn't toggle closed when it should

后端 未结 8 1027
伪装坚强ぢ
伪装坚强ぢ 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: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() })
    

提交回复
热议问题