Bootstrap 的 Modal
一、简介
Modal 就是弹出框,这里 有一个例子。
Modal 的完整代码如下:
<div class="modal fade" tabindex="-1" role="dialog" id="modalOfTriggerViaMarkupAPI" aria-labelledby="modalTitleOfTriggerViaMarkupAPI">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="modalTitleOfTriggerViaMarkupAPI">Modal title</h4>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
默认的 Modal 是隐藏的,让它出现有两种方式:
- 标签 API
- JavaScript 代码
二、通过标签 API
Modal 的代码已经有了,接下来我们要为 Modal 设置 id 并且添加一个按钮,像下面这样:
<button class="btn btn-info" data-toggle="modal" data-target="#modalOfTriggerViaMarkupAPI">Launch Modal Via Markup API</button>
<div class="modal fade" id="modalOfTriggerViaMarkupAPI" tabindex="-1" role="dialog" aria-labelledby="modalTitleOfTriggerViaMarkupAPI">
<!-- some code -->
</div>
当我们点击按钮的时候,Modal 就出现了。起作用的代码是 data-toggle="modal" 和 data-target="#modalOfTriggerViaMarkupAPI",两者缺一不可,它们的意思合起来就是——标签 id 是 modalOfTriggerViaMarkupAPI 的 Modal,我要你显示/隐藏(toggle)。
三、通过 JavaScript 代码
同样要借助 Modal id 和一个按钮:
<button id="btnOfTriggerModalViaJavaScript" class="btn btn-info">Launch Modal Via JavaScript</button>
<div class="modal fade" tabindex="-1" role="dialog" id="modalOfTriggerViaJavaScript">
<!-- some code -->
</div>
让它起作用的 JavaScript 代码如下:
$('#btnOfTriggerModalViaJavaScript').on('click', function triggerModalViaJavaScript () {
$('#modalOfTriggerViaJavaScript').modal('toggle');
})
四、Modal 的事件回调
Modal 可能发生的状态包括显示和隐藏。Bootstrap 针对这两个状态提供了相应的事件回调,代码类似:
$('#modalOfTriggerViaMarkupAPI').on('show.bs.modal', function (e) {
// do something...
})
事件是在 Modal(<div class="modal">) 上触发的,主要有四个:
show.bs.modal:在 Modal 显示时触发。shown.bs.modal:在 Modal 显示之后触发。hide.bs.modal:在 Modal 隐藏时触发。hidden.bs.modal:在 Modal 隐藏之后触发。
五、设备可访问性
为了提高代码的设备可访问性——盲人借助阅读设备同样可以很好地阅读网页内容,我们会给 Modal 添加一些额外代码。
- 在
.modal上:添加role="dialog"和aria-labelledby="..."(值为.modal-title的 id)。 - 在
.modal-dialog上:添加role="document"。
另外,还可以给 .modal 添加 aria-describedby 内容是弹出框的描述。
六、参考链接
http://getbootstrap.com/javascript/#modals
(完)
Bootstrap 的 Modal的更多相关文章
- 解决select2在bootstrap的modal中默认不显示的问题
在Bootstrap中的Modal,select2插件会有不显示,因为其z-index小于modal,还有另外一个问题是,修正z-index之后,select2不会自动失去焦点的问题.代码解决如下: ...
- bootstrap 模态 modal 小例子
bootstrap 模态 modal 小例子 <html> <head> <meta charset="utf-8" /> <title ...
- 利用bootstrap的modal组件自定义alert,confirm和modal对话框
由于浏览器提供的alert和confirm框体验不好,而且浏览器没有提供一个标准的以对话框的形式显示自定义HTML的弹框函数,所以很多项目都会自定义对话框组件.本篇文章介绍自己在项目中基于bootst ...
- bootstrap 模态 modal 小例子【转】
bootstrap 模态 modal 小例子 <html> <head> <meta charset="utf-8" /> <title ...
- 【bootstrap】modal模态框的几种打开方法+问题集锦
第一部分: 关于bootstrap中modal的使用,下面把几种自己用的打开方法展示出来 首先呢,得有个Bootstrap的页面,这里就不说了. 其次呢,得有个modal放在页面中,不管你这段代码加在 ...
- bootstrap的modal弹窗,在多层窗口关闭时只会关闭自窗口,不再关闭父窗口
bootstrap多层modal弹窗时.当子窗口关闭时,所有父窗口一起关闭. 原因是bootstrap在窗口关闭事件委托时,委托给所有窗口. 如源码: this.$element.on('click. ...
- 学会用bootstrap的modal和carousel
bootstrap框架提供了很多好用的javascript组件,可以很方便的实现常用的js效果,比如点击弹出一个div(modal).下拉菜单.旋转木马(carousel或slider),非常适合前端 ...
- 关于bootstrap的modal弹出层嵌套子Modal所引发的血案(转)
原文地址 http://blog.csdn.net/liuxiaogangqq/article/details/51821359 bootstrap的弹出层嵌套有一个问题 ,当子modal关闭时父的m ...
- Bootstrap的$(...).modal is not a function错误
使用模态对话框的时候报错了,$(...).modal is not a function 有点蒙,modal是boostrap的函数,而我已经导入了 然后在pycharm的terminal中看到了这一 ...
- 在BootStrap的modal中使用Select2
bootstrap 3 $.fn.modal.Constructor.prototype.enforceFocus = function() {}; bootstrap4 $.fn.modal.C ...
随机推荐
- wordpress 插件 汉化
http://blog.wpjam.com/article/localizing-a-wordpress-plugin-using-poedit/ 翻译或者说本地化 WordPress 插件和主题可以 ...
- [BZOJ4573][ZJOI2016]大♂森林
bzoj luogu uoj sol \(orz\ \ HJT\ \ dalao\)教会我做这道题. 考虑每两个相邻位置的树的差异. 对于一个1操作(更换生长节点),假设区间是\([l,r]\),那么 ...
- php redis 操作手册
String 类型操作 string是redis最基本的类型,而且string类型是二进制安全的.意思是redis的string可以包含任何数据.比如jpg图片或者序列化的对象 1 $redis-&g ...
- What makes an inferred latch? how To avoid creating inferred latches? when do you know you need latches?
What makes an inferred latch?For combinatorial logic, the output of the circuit is a function of inp ...
- Common 通用类库
/// <summary> /// 传入虚拟路径 返回全路径的html字符串 /// </summary> /// <param name="context&q ...
- phyton方面相关书籍
0基础:<简明PYTHON教程>http://linux.chinaitlab.com/manual/Python_chinese/<与孩子一起学编程>http://book. ...
- Annotation之四:注解中的-Xlint:unchecked和 -Xlint:deprecation
一.-Xlint:unchecked用法 对如下Test.java编译时 package com.dxz.annotation; import java.util.ArrayList; import ...
- JAVA基础知识(16)-----Integer
Integer对象数字格式的字符串转成基本数据类型的方法:1:将该字符串封装成了Integer对象,并调用对象的方法intValue();2:使用Integer.parseInt(numstring) ...
- 第十六章 Velocity工作原理解析(待续)
Velocity总体架构 JJTree渲染过程解析 事件处理机制 常用优化技巧 与JSP比较 设计模式解析之合成模式 设计模式解析之解释器模式
- 11-24网页基础--Js框架及学习思路
第一部分 基本语法: 1.数据类型(字符串.小数.整数.布尔.时间日期)var s="3.14" var n=parsefloat(s) s+=5;var s="abc3 ...