简体   繁体   中英

How can I customize bootstrap dropdown?

I'm a beginner in front-end development, I would like modify the behavior of bootstrap dropdown. In particular I would like to activate the second level with click and rather than hover. Also the first level items should be checkable, now if I click on it the dropdown closes.

I'm using this code: https://bootsnipp.com/snippets/kM4Q

Are you want create a dropdown or select option (look here )?

You have tagged this question with bootstrap-4 tag, but you have added a link with bootstrap 3 files only, so in my answer I choose to use those files. This is a solution for Bootstrap 3 not 4 .

To make that change, you could use jQuery, working with toggleClass function transforming also this CSS rule:

.dropdown-submenu:hover>.dropdown-menu {
    display: block;
}

in this:

.dropdown-submenu > .dropdown-menu.active {
    display: block;
}

To reset to initial situation when you close the dropdown menu, I added this function using Dropdown's event :

  $('#mydropdown').on('hidden.bs.dropdown', function () {
    $('ul', $(this)).removeClass("active");
  })

This is the code:

 $(document).ready(function(){ $('.dropdown-submenu > a').on("click", function(e){ $(this).next('ul').toggleClass("active"); e.preventDefault(); e.stopPropagation(); }); $('#mydropdown').on('hidden.bs.dropdown', function () { $('ul', $(this)).removeClass("active"); }) });
 .dropdown-submenu { position: relative; }.dropdown-submenu>.dropdown-menu { top: 0; left: 100%; margin-top: -6px; margin-left: -1px; -webkit-border-radius: 0 6px 6px 6px; -moz-border-radius: 0 6px 6px; border-radius: 0 6px 6px 6px; }.dropdown-submenu >.dropdown-menu.active { display: block; }.dropdown-submenu>a:after { display: block; content: " "; float: right; width: 0; height: 0; border-color: transparent; border-style: solid; border-width: 5px 0 5px 5px; border-left-color: #ccc; margin-top: 5px; margin-right: -10px; }.dropdown-submenu:hover>a:after { border-left-color: #fff; }.dropdown-submenu.pull-left { float: none; }.dropdown-submenu.pull-left>.dropdown-menu { left: -100%; margin-left: 10px; -webkit-border-radius: 6px 0 6px 6px; -moz-border-radius: 6px 0 6px 6px; border-radius: 6px 0 6px 6px; }
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script> <div class="container"> <div class="row"> <div id="mydropdown"> <div class="dropdown"> <a id="dLabel" role="button" data-toggle="dropdown" class="btn btn-primary" data-target="dropdown-menu" href="#"> Dropdown <span class="caret"></span> </a> <ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu"> <li><a href="#">Some action</a></li> <li><a href="#">Some other action</a></li> <li class="divider"></li> <li class="dropdown-submenu"> <a tabindex="-1" href="#">Hover me for more options</a> <ul class="dropdown-menu"> <li><a tabindex="-1" href="#">Second level</a></li> <li class="dropdown-submenu"> <a href="#">Even More..</a> <ul class="dropdown-menu"> <li><a href="#">3rd level</a></li> <li><a href="#">3rd level</a></li> </ul> </li> <li><a href="#">Second level</a></li> <li><a href="#">Second level</a></li> </ul> </li> </ul> </div> </div> </div> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM