Bootstrap 下拉菜單事件

2018-03-03 10:59 更新

Bootstrap提供各種附加到下拉插件的事件,分別是:

  • show.bs.dropdown:當(dāng)手柄被點(diǎn)擊時(shí)觸發(fā);或下拉手柄收到打開隱藏菜單的請(qǐng)求
  • shown.bs.dropdown:在菜單顯示后觸發(fā)
  • hide.bs.dropdown:在關(guān)閉菜單之前觸發(fā)
  • hidden.bs.dropdown:當(dāng)菜單關(guān)閉時(shí)觸發(fā)

顯示和隱藏事件發(fā)生在完成請(qǐng)求之前而顯示的和隱藏的事件發(fā)生在請(qǐng)求完成時(shí)。

<!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')方法可以切換給定的導(dǎo)航欄或標(biāo)簽式導(dǎo)航的菜單。

<!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>

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)