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 ...
随机推荐
- C# 代码注释和Config文件中,特殊符号的书写方法。
App.config: <?xml version="1.0" encoding="utf-8" ?> <configuration> ...
- webrtc windows下的编译
mkdir webrtc-checkoutcd webrtc-checkout set DEPOT_TOOLS_WIN_TOOLCHAIN=0set GYP_GENERATORS=ninjaset G ...
- 2011年浙大:Graduate Admission
题目描述: It is said that in 2011, there are about 100 graduate schools ready to proceed over 40,000 app ...
- Linux网络工具lsof和netstat
lsof全名为list opened files,即列举系统中已经被打开的文件,基本使用如下: (1) 查看/etc/passwd使用情况 lsof /etc/password (2) 查看监听的so ...
- java代码包装类----------Integer
总结:我的笔试挂了..基础的继承不懂不会.我不知道到底是哪里的问题. 要好好反思 package com.da.ima2; public class jrfk { // int整型不能直接转化为Int ...
- ES之二:Elasticsearch原理
Elasticsearch是最近两年异军突起的一个兼有搜索引擎和NoSQL数据库功能的开源系统,基于Java/Lucene构建.最近研究了一下,感觉 Elasticsearch 的架构以及其开源的生态 ...
- 为什么in_array(0, ['a', 'b', 'c'])返回true
为什么in_array(0, ['a', 'b', 'c'])返回true 目录 1 类型转换 2 严格比较 3 false和null 4 数组中有true 在PHP中,数据会自动转换类型后进行比较. ...
- 部署和调优 2.1 squid正向代理
安装squid yum install -y squid Squid 官方网站为 http://www.squid-cache.org 打开注释掉的 cache_dir ufs / 缓存目录的位置,大 ...
- LAMP 3.4 mysql常用操作-2
给用户授权 > grant all on discuz.* to 'user1'@'192.168.1.%' identified by 'wangshaojun'; 指定库,用户名user1 ...
- linux命令-passwd
修改密码 #passwd 新密码 重新输入密码 #passwd dennywang ////命令+用户名 ////////////////////////////////////////////// ...