Bootstrap下拉菜单

时间:2014-02-19 17:45:22

标签: twitter-bootstrap-3

我知道有很多代码exmaples /问题。我搜索了很多,并试图复制/粘贴许多代码。没有任何效果。很抱歉,如果重复或其他什么。

无论如何,我正在尝试在我的固定导航栏中添加一个bootstrap Dropdown菜单。 我不希望激活下拉列表,因此我删除了插入符号。

这是我(部分)我的代码:

  <div class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <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="http://getbootstrap.com/examples/navbar-fixed-top/#">SDK Tests Status</a>
    </div>
    <div class="navbar-collapse collapse">
      <span class="glyphicon glyphicon-folder-open room-icn"></span> 
        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Rooms</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 class="divider"></li>
            <li class="dropdown-header">Nav header</li>
            <li><a href="#">Separated link</a></li>
            <li><a href="#">One more separated link</a></li>
          </ul>


    </div><!--/.nav-collapse -->
  </div>
</div>

任何?谢谢!

1 个答案:

答案 0 :(得分:1)

我不能相信这一点。 Ryan Olson有一个教程here。将CSS添加到.css文件或您的脑袋。在</body>之前将JavaScript添加到页面的最底部。将HTML放在页面正文中。

CSS:

.navbar-nav .dropdown:hover > .dropdown-menu { 
  display: block; 
}
.navbar-nav .dropdown:hover > a[data-toggle=dropdown] + .dropdown-menu { 
  display: none;
}
.navbar-nav .dropdown.open > a[data-toggle=dropdown] + .dropdown-menu { 
  display: block; 
}

JavaScript的:

<script type="text/javascript">
$(function () {
  var $win = $(window);
  $win.resize(function () {
      if ($win.width() > 768)
          $(".navbar-nav > .dropdown > a").attr("data-toggle", "");
      else
          $(".navbar-nav > .dropdown > a").attr("data-toggle", "dropdown");
  }).resize();
  $(".dropdown-menu").find("input, button").each(function () {
      $(this).click(function (e) {
          e.stopPropagation();
      });
  });
});
</script>

HTML:

 <div class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <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="http://getbootstrap.com/examples/navbar-fixed-top/#">SDK Tests Status</a>
    </div>
    <div class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-folder-open room-icn"></span> Rooms</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 class="divider"></li>
            <li class="dropdown-header">Nav header</li>
            <li><a href="#">Separated link</a></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
     </div><!--/.nav-collapse -->
  </div>
</div>

整页:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 
        <meta charset="utf-8">
        <link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <!--[if lt IE 9]>
          <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <style type="text/css">
             .navbar-nav .dropdown:hover > .dropdown-menu { 
              display: block; 
            }
            .navbar-nav .dropdown:hover > a[data-toggle=dropdown] + .dropdown-menu { 
              display: none;
            }
            .navbar-nav .dropdown.open > a[data-toggle=dropdown] + .dropdown-menu { 
              display: block; 
            }
        </style>    
    </head>

    <body>

    <div class="navbar navbar-default navbar-fixed-top" role="navigation">
          <div class="container">
            <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="http://getbootstrap.com/examples/navbar-fixed-top/#">SDK Tests Status</a>
            </div>
            <div class="navbar-collapse collapse">
              <ul class="nav navbar-nav">
                <li class="dropdown">
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-folder-open room-icn"></span> Rooms</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 class="divider"></li>
                    <li class="dropdown-header">Nav header</li>
                    <li><a href="#">Separated link</a></li>
                    <li><a href="#">One more separated link</a></li>
                  </ul>
                </li>
              </ul>
             </div><!--/.nav-collapse -->
          </div>
        </div>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

    <script type="text/javascript">
        $(function () {
          var $win = $(window);
          $win.resize(function () {
              if ($win.width() > 768)
                  $(".navbar-nav > .dropdown > a").attr("data-toggle", "");
              else
                  $(".navbar-nav > .dropdown > a").attr("data-toggle", "dropdown");
          }).resize();
          $(".dropdown-menu").find("input, button").each(function () {
              $(this).click(function (e) {
                  e.stopPropagation();
              });
          });
        });
    </script>
    </body>
</html>

Here is a bootply of it.