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

前端 未结 15 794
暗喜
暗喜 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:19

    Alternatively here's a simple jQuery solution:

    $('#menu-main > li > .dropdown-toggle').click(function () {
        window.location = $(this).attr('href');
    });
    
    0 讨论(0)
  • 2020-12-07 10:19

    This 100% works:

    HTML:

    <li class="dropdown">
              <a href="https://www.bkweb.co.in/" class="dropdown-toggle" >bkWeb.co.in Dropdown <span class="caret"></span></a>
              <ul class="dropdown-menu">
                <li><a href="#">Action</a></li>
                <li><a href="#">Another action</a></li>
                <li><a href="#">Something else here</a></li>
                <li role="separator" class="divider"></li>
                <li><a href="#">Separated link</a></li>
                <li role="separator" class="divider"></li>
                <li><a href="#">One more separated link</a></li>
              </ul>
            </li>
    

    CSS:

    @media (min-width:991px){
      ul.nav li.dropdown:hover ul.dropdown-menu {
        display: block;
      }
    }
    

    EXAMPLE in CODEPEN

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

    Anyone arriving here who wants the quick answer to this problem. Replace the "Dropdown.prototype.toggle" function in your bootstrap.js (or dropdown.js) with the following:

      Dropdown.prototype.toggle = function (e) {
    var $this = $(this)
    
    if ($this.is('.disabled, :disabled')) return
    
    var $parent  = getParent($this)
    var isActive = $parent.hasClass('open')
    
    clearMenus()
    
    if (!isActive) {
        if ('ontouchstart' in document.documentElement && !$parent.closest('.navbar-nav').length) {
            // if mobile we use a backdrop because click events don't delegate
            $('<div class="dropdown-backdrop"/>').insertAfter($(this)).on('click', clearMenus)
        }
    
        var relatedTarget = { relatedTarget: this }
        $parent.trigger(e = $.Event('show.bs.dropdown', relatedTarget))
    
        if (e.isDefaultPrevented()) return
    
        $parent
          .toggleClass('open')
          .trigger('shown.bs.dropdown', relatedTarget)
    
        $this.focus()
    }
    else
    {
        var href = $this.attr("href").trim();
    
        if (href != undefined && href != " javascript:;")
            window.location.href = href;
    }
    
    return false
      }
    

    On the second click (ie: if the menu item has the class "open") it will first check if the href is undefined or set to "javascript:;" before sending you along your merry way.

    Enjoy!

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

    1: remove dropdown-trigger:

    data-toggle="dropdown"
    

    2: add this your css

    .dropdown:hover .dropdown-menu {
        display: block;
    }
    .dropdown-menu {
        margin-top: 0px;
    }
    

    posted for the people how stumbled upon this

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

    Here is my solution which uses JQuery in your header, and works on Mobile.

    On mobile the top links require two taps: one to dropdown the menu and one to go to its link.

    $(function(){
      $('.dropdown-toggle').click(
        function(){
          if ($(this).next().is(':visible')) {
            location.href = $(this).attr('href');;
          }
         });
      });
    });

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

    No need of use addition CSS/JS (Tested)

    1. data-toggle="dropdown" - for Clickable (can use Mobile as well web)
    2. data-hover="dropdown" - for Hover (web only, because mobile doesn't have feature HOVER)

    Works fine on Mobile as well :)


    Code Example for Clickable(data-toggle="dropdown")

    /*!
     * this CSS code just  for snippet preview purpose. Please omit when using  it. 
     */
    
    #bs-example-navbar-collapse-1 ul li {
      float: left;
    }
    
    #bs-example-navbar-collapse-1 ul li ul li {
      float: none !important;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
    <div id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li>
          <a class="" href="">Home</a>
        </li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                    subnav1
                </a>
          <ul class="dropdown-menu">
            <li><a href="">Sub1</a></li>
            <li><a href="">Sub2</a></li>
            <li><a href="">Sub3</a></li>
            <li><a href="">Sub4</a></li>
            <li><a href="">Sub5</a></li>
            <li><a href="">Sub6</a></li>
          </ul>
          <div class="clear"></div>
        </li>
        <li class="dropdown">
          <a href="" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">
                subnav2
                </a>
          <ul class="dropdown-menu">
            <li><a href="">Sub1</a></li>
            <li><a href="">Sub2</a></li>
            <li><a href="">Sub3</a></li>
            <li><a href="">Sub4</a></li>
            <li><a href="">Sub5</a></li>
            <li><a href="">Sub6</a></li>
          </ul>
          <div class="clear"></div>
        </li>
      </ul>
    </div>
    
    <br>
    <br>
    <p><b>Please Note:</b> added css code not related to Bootstrap navigation. It's just for snippet preview purpose </p>

    Output

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