快速解決bootstrap下拉菜單無法隱藏的問題
下拉菜單的兩種實現(xiàn)
想必大家都知道,bootstrap為我們提供了一個下拉菜單的組件,官方也為我們提供兩種使用方法
1.標簽指定data-toggle
<div class="dropdown"> <button type="button" class="btn dropdown-toggle" id="dropdownMenu1" data-toggle="dropdown">主題 <span class="caret"></span> </button> <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1"> <li role="presentation"> <a role="menuitem" tabindex="-1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Java</a> </li> <li role="presentation"> <a role="menuitem" tabindex="-1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >數(shù)據(jù)挖掘</a> </li> <li role="presentation"> <a role="menuitem" tabindex="-1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >數(shù)據(jù)通信/網(wǎng)絡</a> </li> <li role="presentation" class="divider"></li> <li role="presentation"> <a role="menuitem" tabindex="-1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >分離的鏈接</a> </li> </ul> </div>
2.js調用dropdown(‘toggle')方法
<div class="dropdown"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="dropdown-toggle" id="dropdownMenu1" > 主題 <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1"> <li role="presentation"> <a role="menuitem" tabindex="-1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Java</a> </li> <li role="presentation"> <a role="menuitem" tabindex="-1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >數(shù)據(jù)挖掘</a> </li> <li role="presentation"> <a role="menuitem" tabindex="-1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >數(shù)據(jù)通信/網(wǎng)絡</a> </li> </ul> <button id="test">test</button> </div> </body> <script> $(function() { $("#test").click(function() { $(".dropdown-toggle").dropdown('toggle'); }) }) </script>
第二種方法有個很嚴重的問題
就是,下拉菜單開啟后,官方并沒有提供將它隱藏的方法,網(wǎng)上的方法死活說再次調用dropdown(‘toggle')。。。反正我不行。
那么我想用點擊之外來開啟下拉菜單呢?(必須得使用JS來控制)
我想到一個方法,使用jQuery提供的一個效果slideUp(),slideDown()
<body> <div class="dropdown"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="dropdown-toggle" id="dropdownMenu1" > 主題 <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1"> <li role="presentation"> <a role="menuitem" tabindex="-1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Java</a> </li> <li role="presentation"> <a role="menuitem" tabindex="-1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >數(shù)據(jù)挖掘</a> </li> <li role="presentation"> <a role="menuitem" tabindex="-1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >數(shù)據(jù)通信/網(wǎng)絡</a> </li> <li role="presentation" class="divider"></li> <li role="presentation"> <a role="menuitem" tabindex="-1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >分離的鏈接</a> </li> </ul> <button id="test">test</button> <button id="test1">test1</button> </div> </body> <script> $(function() { $("#test").click(function() { $(".dropdown-menu").slideUp(); }) $("#test1").click(function() { $(".dropdown-menu").slideDown(); }) }) </script>
好了現(xiàn)在能實現(xiàn)開和關了,但是速度有點慢?沒事slide可以通過參數(shù)調節(jié)速度甚至還有回調函數(shù)來替代$(selector).on("hidden.bs.dropdown', function () {})(對下拉菜單收回時間的監(jiān)聽),滿分!
$(selector).slideUp(speed,callback)
speed 可選。規(guī)定元素從可見到隱藏的速度(或者相反)。默認為 “normal”。 可能的值:
毫秒 (比如 1500)
“slow”
“normal”
“fast”
在設置速度的情況下,元素從可見到隱藏的過程中,會逐漸地改變其高度(這樣會創(chuàng)造滑動效果)。
callback
可選。slideUp 函數(shù)執(zhí)行完之后,要執(zhí)行的函數(shù)。 除非設置了 speed 參數(shù),否則不能設置該參數(shù)。
以上這篇快速解決bootstrap下拉菜單無法隱藏的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
JavaScript高級程序設計 閱讀筆記(十五) 瀏覽器中的JavaScript
Window對象對操作瀏覽器窗口非常有用,開發(fā)者可以移動或調整瀏覽器窗口的大小2012-08-08小程序云開發(fā)實現(xiàn)數(shù)據(jù)庫異步操作同步化
這篇文章主要為大家詳細介紹了小程序云開發(fā)實現(xiàn)數(shù)據(jù)庫異步操作同步化,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05