快速解决bootstrap下拉菜单无法隐藏的问题


Posted in Javascript onAugust 10, 2018

下拉菜单的两种实现

想必大家都知道,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" >数据挖掘</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" >数据通信/网络</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" >数据挖掘</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" >数据通信/网络</a>
    </li>
  </ul>
<button id="test">test</button>
</div>
</body>

<script>
  $(function() {
    $("#test").click(function() {
      $(".dropdown-toggle").dropdown('toggle');
    })
  })
</script>

第二种方法有个很严重的问题

就是,下拉菜单开启后,官方并没有提供将它隐藏的方法,网上的方法死活说再次调用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" >数据挖掘</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" >数据通信/网络</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>

好了现在能实现开和关了,但是速度有点慢?没事slide可以通过参数调节速度甚至还有回调函数来替代$(selector).on("hidden.bs.dropdown', function () {})(对下拉菜单收回时间的监听),满分!

$(selector).slideUp(speed,callback)

speed 可选。规定元素从可见到隐藏的速度(或者相反)。默认为 “normal”。 可能的值:

毫秒 (比如 1500)

“slow”
“normal”
“fast”

在设置速度的情况下,元素从可见到隐藏的过程中,会逐渐地改变其高度(这样会创造滑动效果)。

callback

可选。slideUp 函数执行完之后,要执行的函数。 除非设置了 speed 参数,否则不能设置该参数。

以上这篇快速解决bootstrap下拉菜单无法隐藏的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
关于JavaScript的gzip静态压缩方法
Jan 05 Javascript
javascript 数组排序函数sort和reverse使用介绍
Nov 21 Javascript
javascript 实现字符串反转的三种方法
Nov 23 Javascript
js动态添加onclick事件可传参数与不传参数
Jul 29 Javascript
window.location 对象所包含的属性
Oct 10 Javascript
javascript设置连续两次点击按钮时间间隔的方法
Oct 28 Javascript
jQuery实现可用于博客的动态滑动菜单完整实例
Sep 17 Javascript
微信小程序 JS动态修改样式的实现代码
Feb 10 Javascript
JavaScript实现数字前补“0”的五种方法示例
Jan 03 Javascript
JS Generator 函数的含义与用法实例总结
Apr 08 Javascript
VSCode Vue开发推荐插件和VSCode快捷键(小结)
Aug 08 Javascript
js+canvas实现图片格式webp/png/jpeg在线转换
Aug 22 Javascript
详解Vue+axios+Node+express实现文件上传(用户头像上传)
Aug 10 #Javascript
快速解决select2在bootstrap模态框中下拉框隐藏的问题
Aug 10 #Javascript
Bootstrap模态对话框中显示动态内容的方法
Aug 10 #Javascript
解决bootstrap-select 动态加载数据不显示的问题
Aug 10 #Javascript
使用bootstrap实现下拉框搜索功能的实例讲解
Aug 10 #Javascript
bootstrap里bootstrap动态加载下拉框的实例讲解
Aug 10 #Javascript
bootstrap下拉框动态赋值方法
Aug 10 #Javascript
You might like
example2.php
2006/10/09 PHP
自己写的兼容低于PHP 5.5版本的array_column()函数
2014/10/24 PHP
深入解析PHP的Laravel框架中的event事件操作
2016/03/21 PHP
JS 无限级 Select效果实现代码(json格式)
2011/08/30 Javascript
jQuery版仿Path菜单效果
2011/12/15 Javascript
js控制淡入淡出示例代码
2013/11/12 Javascript
用js+iframe形成页面的一种遮罩效果的具体实现
2013/12/31 Javascript
JS获取下拉列表所选中的TEXT和Value的实现代码
2014/01/11 Javascript
常用的jquery模板插件——jQuery Boilerplate介绍
2014/09/23 Javascript
JSONP和批量操作功能的实现方法
2016/08/21 Javascript
JS扩展类,克隆对象与混合类实例分析
2016/11/26 Javascript
详解webpack和webpack-simple中如何引入css文件
2017/06/28 Javascript
微信小程序使用video组件播放视频功能示例【附源码下载】
2017/12/08 Javascript
ant-design-vue按需加载的坑的解决
2020/05/14 Javascript
VUE : vue-cli中去掉路由中的井号#操作
2020/09/04 Javascript
JavaScript实现打字游戏
2021/02/19 Javascript
Python程序设计入门(5)类的使用简介
2014/06/16 Python
关于Python 的简单栅格图像边界提取方法
2019/07/05 Python
使用Python实现分别输出每个数组
2019/12/06 Python
django ObjectDoesNotExist 和 DoesNotExist的用法
2020/07/09 Python
selenium切换标签页解决get超时问题的完整代码
2020/08/30 Python
Fenty Beauty官网:蕾哈娜创立的美妆品牌
2021/01/07 全球购物
什么是WEB控件?使用WEB控件有哪些优势?
2012/01/21 面试题
金属材料工程毕业生个人的自我评价
2013/11/28 职场文书
医学专业本科毕业生自我鉴定
2013/12/28 职场文书
运动会领导邀请函
2014/01/10 职场文书
校园文明标语
2014/06/13 职场文书
禁毒宣传活动总结
2014/08/26 职场文书
四风问题对照检查材料
2014/09/22 职场文书
政府班子四风问题整改措施
2014/10/04 职场文书
领导干部查摆“四风”问题自我剖析材料思想汇报
2014/10/05 职场文书
歼十出击观后感
2015/06/11 职场文书
莫言获奖感言(全文)
2015/07/31 职场文书
2016年圣诞节义工活动总结
2016/04/01 职场文书
python自动计算图像数据集的RGB均值
2021/06/18 Python
Nginx报错104:Connection reset by peer问题的解决及分析
2022/07/23 Servers