Bootstrap 3: how to make head of dropdown link clickable in navbar

前端 未结 15 793
暗喜
暗喜 2020-12-07 09:23

I\'m using the default navbar and a couple of the list items are dropdowns. I\'m not able to click the link that triggers the dropdown. I know that I could just add a duplic

相关标签:
15条回答
  • 2020-12-07 10:24

    Here is a small hack based on Bootstrap 3.3 using a bit jQuery.

    A click on a opened dropdown-menu executes the link.

    $('li.dropdown').on('click', function() {
        var $el = $(this);
        if ($el.hasClass('open')) {
            var $a = $el.children('a.dropdown-toggle');
            if ($a.length && $a.attr('href')) {
                location.href = $a.attr('href');
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-07 10:24

    I know this is a little old, but I recently came across this while looking for a similar solution. Relying on hover events isn't good for responsive design, and especially terrible on mobile/touch screens. I ended up making a small edit to the dropdown.js file the allows you to click the menu item to open the menu and if you click the menu item again it will follow it.

    The nice thing about this is it doesn't rely on hover at all and so it still works really nicely on a touch screen.

    I've posted it here: https://github.com/mrhanlon/twbs-dropdown-doubletap/blob/master/js/dropdown-doubletap.js

    Hope that helps!

    0 讨论(0)
  • 2020-12-07 10:27

    This enables the link in the top-level menu of the dropdown when it's opened and disables it when closed, with the only drawback of apparently having to "tap" twice outside of the dropdown to close it in mobile devices.

    $(document).on("page:load", function(){
        $('body').on('show.bs.dropdown', function (e) {
            $(e.relatedTarget).addClass('disabled')
        });
        $('body').on('hide.bs.dropdown', function (e) {
            $(e.relatedTarget).removeClass('disabled')
        });
    });
    

    Note this assumes the "standard" markup and Turbolinks (Rails). You can try with $(document).ready(...)

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