Bootstrap Dropdown Plugin
Bootstrap Dropdown This chapter explained dropdowns but did not cover the interactive part; this chapter will explain dropdown interaction in detail. With the Dropdown plugin, you can add dropdown menus to any component (such as navbars, tabs, pill navigation, buttons, etc.).
If you want to reference the plugin's functionality separately, you need to include dropdown.js. Or, as Bootstrap Plugins Overview chapter mentions, you can include bootstrap.js or the compressed version bootstrap.min.js。
Usage
You can toggle the hidden content of the Dropdown plugin:
-
Via data attributes: add to a link or button data-toggle="dropdown" to toggle the dropdown, as shown below:
If you need to keep the link intact (useful when JavaScript is not enabled in the browser), use data-target attribute instead of href="#":
$('.dropdown-toggle').dropdown()
Example
Inside a Navbar
The following example demonstrates the use of a dropdown inside a navbar:
Example
<nav class=">navbar navbar-default
"
role
=
"
navigation
"
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="">Runoob Tutorial
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="">Separated link
a
li
<li class="divider" li>
<li>
<a href="">Another separate link
a
li
ul
li
ul
div
div
nav
Try it »
The result is as follows:
Inside Tabs
The following example demonstrates the use of a dropdown inside tabs:
Example
<
p
Tabs with Dropdowns
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="">Separated link
a
li
ul
li
<li>
<a href="" PHP a li ul>Try it »
The result is as follows:
Option
No options.
Method
The dropdown toggle has a simple method to show or hide a dropdown.
$().dropdown('toggle')
Example
The following example demonstrates the use of the Dropdown plugin methods:
Example
<nav class=">navbar navbar-default
"
role
=
"
navigation
"
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="">Runoob Tutorial
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" data-toggle="dropdown" 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="">Separated link
a
li
<li class="divider" li>
<li>
<a href="">Another separate link
a
li
ul
li
ul
div
div
nav
div
<
script
$(function() { $(".dropdown-toggle").dropdown('toggle'); });
script
Try it »
The result is as follows: