导航栏中的引导下拉菜单未打开

Posted

技术标签:

【中文标题】导航栏中的引导下拉菜单未打开【英文标题】:Bootstraps dropdown-menu in navbar does not open 【发布时间】:2018-01-04 19:14:24 【问题描述】:

我的 navbar 中的下拉菜单要么不打开(如果我省略 open),要么一直保持打开状态(当我像在下面的版本中那样添加 open 时)。即使我省略了打开,它怎么能打开,或者我怎样才能让它再次关闭? (那里有 jQuery 和 bootstrapcdn 的链接):

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<body data-spy="scroll" data-target=".navbar">
  <!--navbar starts here-->
  <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container-fluid">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse"
  data-target=".navbar-collapse">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>  
        <a class="navbar-brand" href="#">
          <img  src="...">
        </a>

        <div class="projectName"> 
          <a href="#">ДЖУЛИН</a>
        </div>
      </div>

      <div class="navbar-collapse collapse">
        <ul class="nav navbar-nav navbar-right">
          <li class="active"> <a href="#home"> HOME </a> </li>
          <li class="dropdown open" role="presentation">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown"       role="button" aria-haspopup="true" aria-expanded="false"> WORK <span    class="caret"></span>
            </a>
            <ul class="dropdown-menu">
              <li><a href="#work1" class="scroll-nav">Work 1</a></li>
              <li><a href="#work2" class="scroll-nav">Work 2</a></li>
            </ul>
         </li>

         <li class="active"> <a href="#about"> ABOUT </a> </li>
         <li class="active"> <a href="#faq"> FAQ </a> </li>
         <li class="active"> <a href="#contact"> CONTACT </a></li>
      </ul>

    </div> <!--collapse-->
  </div> <!--header-->
 </div> <!-- end nav container -->
</nav> <!-- end navbar-->

【问题讨论】:

【参考方案1】:

这里有一个解决方案https://jsfiddle.net/08xp0q92/

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<body data-spy="scroll" data-target=".navbar">
  <!--navbar starts here-->
  <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container-fluid">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse"
  data-target=".navbar-collapse">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>  
        <a class="navbar-brand" href="#">
          <img  src="...">
        </a>

        <div class="projectName"> 
          <a href="#">ДЖУЛИН</a>
        </div>
      </div>

      <div class="navbar-collapse collapse">
        <ul class="nav navbar-nav navbar-right">
          <li class="active"> <a href="#home"> HOME </a> </li>
          <li class="dropdown open" role="presentation">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> WORK <span    class="caret"></span>
            </a>
            <ul class="dropdown-menu">
              <li><a href="#work1" class="scroll-nav">Work 1</a></li>
              <li><a href="#work2" class="scroll-nav">Work 2</a></li>
            </ul>
         </li>

         <li class="active"> <a href="#about"> ABOUT </a> </li>
         <li class="active"> <a href="#faq"> FAQ </a> </li>
         <li class="active"> <a href="#contact"> CONTACT </a></li>
      </ul>

    </div> <!--collapse-->
  </div> <!--header-->
 </nav> <!-- end nav container -->
</body> <!-- end navbar-->

您必须包含 bootstrap.js

【讨论】:

以上是关于导航栏中的引导下拉菜单未打开的主要内容,如果未能解决你的问题,请参考以下文章

Twitter引导导航栏和下拉菜单-不调整大小

如何在折叠的导航栏中默认打开 Bootstrap 3 下拉菜单

下拉菜单在导航栏中无法按预期工作

导航栏中的用户菜单下拉菜单

从外部按钮打开引导下拉菜单

如何在 twitter 引导下拉菜单中使用悬停图像