Bootstrap 下拉菜單 這一章講解了下拉菜單,但是沒(méi)有涉及到交互部分,本章將具體講解下拉菜單的交互。使用下拉菜單(Dropdown)插件,您可以向任何組件(比如導(dǎo)航欄、標(biāo)簽頁(yè)、膠囊式導(dǎo)航菜單、按鈕等)添加下拉菜單。
如果您想要單獨(dú)引用該插件的功能,那么您需要引用 dropdown.js?;蛘撸?Bootstrap 插件概覽 一章中所提到,您可以引用 bootstrap.js 或壓縮版的 bootstrap.min.js。
Bootstrap 中下拉菜單的切換有以下兩種方法:
您可以切換下拉菜單(Dropdown)插件的隱藏內(nèi)容:
<div class="dropdown">
<a data-toggle="dropdown" href="#">下拉菜單(Dropdown)觸發(fā)器</a>
<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
...
</ul>
</div>
如果您需要保持鏈接完整(在瀏覽器不啟用 JavaScript 時(shí)有用),請(qǐng)使用 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>
$('.dropdown-toggle').dropdown()
下面的實(shí)例演示了在導(dǎo)航欄內(nèi)的下拉菜單的用法:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 實(shí)例 - 默認(rèn)的導(dǎo)航欄</title>
<link rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="stylesheet">
<script src="http://cdn.bootcss.com/jquery/2.1.1/jquery.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="http://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<a class="navbar-brand" href="#">W3Cschool</a>
</div>
<div>
<ul class="nav navbar-nav">
<li class="active"><a href="#">iOS</a></li>
<li><a href="#">SVN</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Java
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="#">jmeter</a></li>
<li><a href="#">EJB</a></li>
<li><a href="#">Jasper Report</a></li>
<li class="divider"></li>
<li><a href="#">分離的鏈接</a></li>
<li class="divider"></li>
<li><a href="#">另一個(gè)分離的鏈接</a></li>
</ul>
</li>
</ul>
</div>
</nav>
</body>
</html>
結(jié)果如下所示:
下面的實(shí)例演示了在標(biāo)簽頁(yè)內(nèi)的下拉菜單的用法:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 實(shí)例 - 帶有下拉菜單的標(biāo)簽頁(yè)</title>
<link rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="stylesheet">
<script src="http://cdn.bootcss.com/jquery/2.1.1/jquery.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="http://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<p>帶有下拉菜單的標(biāo)簽頁(yè)</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">SVN</a></li>
<li><a href="#">iOS</a></li>
<li><a href="#">VB.Net</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
Java <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="#">Swing</a></li>
<li><a href="#">jMeter</a></li>
<li><a href="#">EJB</a></li>
<li class="divider"></li>
<li><a href="#">分離的鏈接</a></li>
</ul>
</li>
<li><a href="#">PHP</a></li>
</ul>
</body>
</html>
結(jié)果如下所示:
沒(méi)有選項(xiàng)。
下拉菜單切換有一個(gè)簡(jiǎn)單的方法用來(lái)顯示或隱藏下拉菜單。
$().dropdown('toggle')
下面的實(shí)例演示了下拉菜單(Dropdown)插件方法的用法:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 實(shí)例 - 下拉菜單(Dropdown)插件方法</title>
<link rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="stylesheet">
<script src="http://cdn.bootcss.com/jquery/2.1.1/jquery.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="http://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<a class="navbar-brand" href="#">W3Cschool</a>
</div>
<div id="myexample">
<ul class="nav navbar-nav">
<li class="active"><a href="#">iOS</a></li>
<li><a href="#">SVN</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle">Java <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a id="action-1" href="#">
jmeter</a>
</li>
<li><a href="#">EJB</a></li>
<li><a href="#">Jasper Report</a></li>
<li class="divider"></li>
<li><a href="#">分離的鏈接</a></li>
<li class="divider"></li>
<li><a href="#">另一個(gè)分離的鏈接</a></li>
</ul>
</li>
</ul>
</div>
</nav>
</div>
<script>
$(function(){
$(".dropdown-toggle").dropdown('toggle');
});
</script>
</body>
</html>
結(jié)果如下所示:
更多建議: