1.下面的实例演示了在导航栏内和标签内的下拉菜单的用法:
<!DOCTYPE HTML>
<html>
<head>
<link href="/stylesheets/bootstrap.min.css" rel="stylesheet" >
<script src="/scripts/jquery.min.js"></script>
<script src="/scripts/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-default" role="navigation">
<div>
<ul class="nav navbar-nav">
<li class="active"><a href="javascripts:void(0);">iOS</a></li>
<li><a href="javascripts:void(0);">SVN</a></li>
<li class="dropdown">
<a href="javascripts:void(0);" class="dropdown-toggle" data-toggle="dropdown">
Java
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="javascripts:void(0);">jmeter</a></li>
<li><a href="javascripts:void(0);">EJB</a></li>
<li><a href="javascripts:void(0);">Jasper Report</a></li>
<li class="divider"></li>
<li><a href="javascripts:void(0);">分离的链接</a></li>
<li class="divider"></li>
<li><a href="javascripts:void(0);">另一个分离的链接</a></li>
</ul>
</li>
</ul>
</div>
</nav>
<p>带有下拉菜单的标签页</p>
<ul class="nav nav-tabs">
<li class="active"><a href="javascripts:void(0);">Home</a></li>
<li><a href="javascripts:void(0);">SVN</a></li>
<li><a href="javascripts:void(0);">iOS</a></li>
<li><a href="javascripts:void(0);">VB.Net</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="javascripts:void(0);">
Java <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="javascripts:void(0);">Swing</a></li>
<li><a href="javascripts:void(0);">jMeter</a></li>
<li><a href="javascripts:void(0);">EJB</a></li>
<li class="divider"></li>
<li><a href="javascripts:void(0);">分离的链接</a></li>
</ul>
</li>
<li><a href="javascripts:void(0);">PHP</a></li>
</ul>
</body>
</html>
2.用法
可以切换下拉菜单(Dropdown)插件的隐藏内容:
通过 data 属性:向链接或按钮添加 data-toggle="dropdown" 来切换下拉菜单,如下所示:
- <div class="dropdown">
- <a data-toggle="dropdown" href="#">下拉菜单(Dropdown)触发器</a>
- <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
- ...
- </ul>
- </div>
如果您需要保持链接完整(在浏览器不启用 JavaScript 时有用),请使用 data-target 属性代替 href="#":
- <div class="dropdown">
- <a id="dLabel" role="button" data-toggle="dropdown" data-target="#" href="/page.html">
- 下拉菜单(Dropdown) <span class="caret"></span>
- </a>
- <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
- ...
- </ul>
- </div>
通过 JavaScript:通过 JavaScript 调用下拉菜单切换,请使用下面的方法:
- $('.dropdown-toggle').dropdown()
例子:
<!DOCTYPE HTML>
<html>
<head>
<link href="/stylesheets/bootstrap.min.css" rel="stylesheet" >
<script src="/scripts/jquery.min.js"></script>
<script src="/scripts/bootstrap.min.js"></script>
</head>
<body>
<div class="dropdown">
<a data-toggle="dropdown" href="#">下拉菜单(Dropdown)触发器(点击)</a>
<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
<li class="active"><a href="javascripts:void(0);">iOS</a></li>
<li><a href="javascripts:void(0);">SVN</a></li>
</ul>
</div>
<div class="dropdown">
<a id="dLabel" role="button" data-toggle="dropdown" data-target="#" href="/page.html">
下拉菜单(Dropdown) <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
<li class="active"><a href="javascripts:void(0);">iOS</a></li>
<li class="active"><a href="javascripts:void(0);">iOS</a></li>
</ul>
</div>
</body>
</html>
3.方法
下拉菜单切换有一个简单的方法用来显示或隐藏下拉菜单。
- $().dropdown('toggle')