BootStrap3 keep the dropdown menu open after click on the item

后端 未结 3 555
别那么骄傲
别那么骄傲 2020-11-29 11:50

I\'m using bootstrap3.0, with it excellent drop-down menu.

If I click out side of the drop-down menu the menu will disappear, and this is quite right.

but

相关标签:
3条回答
  • 2020-11-29 12:26

    The accepted answer is very helpful. I want to provide another perspective - when a drop down menu should stay open when only certain items are clicked.

    // A utility for keeping a Bootstrap drop down menu open after a link is
    // clicked
    //
    // Usage:
    //
    //   <div class="dropdown">
    //     <a href="" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
    //       Dropdown trigger <span class="caret"></span>
    //     </a>
    //
    //     <ul class="dropdown-menu" aria-labelledby="dLabel">
    //       <li><a href="">Edit</a></li>
    //       <li><a href="" keep-menu-open="true">Delete</a></li>
    //     </ul>
    //  </div>
    
    $(".dropdown .dropdown-menu a").on("click", function(e) {
      var keepMenuOpen = $(this).data("keep-menu-open"),
          $dropdown = $(this).parents(".dropdown");
    
      $dropdown.data("keep-menu-open", keepMenuOpen);
    });
    
    $(".dropdown").on("hide.bs.dropdown", function(e) {
      var keepMenuOpen = $(this).data("keep-menu-open");
    
      $(this).removeData("keep-menu-open");
    
      return keepMenuOpen !== true;
    });
    
    0 讨论(0)
  • 2020-11-29 12:37

    In vanilla JS

    document.getElementById('myDropdown').addEventListener('click', function (event) {
        event.stopPropagation();
      });
    
    0 讨论(0)
  • 2020-11-29 12:50

    Here is one way to keep the dropdown open after click...

    $('#myDropdown').on('hide.bs.dropdown', function () {
        return false;
    });
    

    Demo: http://www.bootply.com/116350

    Another option is to handle the click event like this..

    $('#myDropdown .dropdown-menu').on({
        "click":function(e){
          e.stopPropagation();
        }
    });
    

    Demo: http://www.bootply.com/116581

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