Bootstrap 下拉菜单事件
2018-03-03 10:59 更新
Bootstrap提供各种附加到下拉插件的事件,分别是:
- show.bs.dropdown:当手柄被点击时触发;或下拉手柄收到打开隐藏菜单的请求
- shown.bs.dropdown:在菜单显示后触发
- hide.bs.dropdown:在关闭菜单之前触发
- hidden.bs.dropdown:当菜单关闭时触发
显示和隐藏事件发生在完成请求之前而显示的和隐藏的事件发生在请求完成时。
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".myDropdownHandle").dropdown("toggle");
});
</script>
</head>
<body style="margin:30px">
<div class="dropdown" id="myDropdown">
<a class="myDropdownHandle" data-toggle="dropdown" data-target="#" href="#">
Dropdown <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>
<li><a href="#">Link 4</a></li>
</ul>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#myDropdown").on("show.bs.dropdown", function () {
console.log("Opening dropdown..");
});
$("#myDropdown").on("shown.bs.dropdown", function () {
console.log("Dropdown opened..");
});
$("#myDropdown").on("hide.bs.dropdown", function () {
console.log("Hiding dropdown..");
});
$("#myDropdown").on("hidden.bs.dropdown", function () {
console.log("Dropdown hidden..");
});
});
</script>
</body>
</html>
$().dropdown('toggle')
$().dropdown('toggle')方法可以切换给定的导航栏或标签式导航的菜单。
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".dropdown-toggle").dropdown();
});
</script>
<style type="text/css">
.bs-example{
margin: 20px;
}
</style>
</head>
<body>
<div class="bs-example">
<div class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
</div>
</body>
</html>
以上内容是否对您有帮助:
更多建议: