一、简介

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">&times;</span>
</button>
<h4 class="modal-title" id="modalTitleOfTriggerViaMarkupAPI">Modal title</h4>
</div>
<div class="modal-body">
<p>One fine body&hellip;</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 是隐藏的,让它出现有两种方式:

  1. 标签 API
  2. 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",两者缺一不可,它们的意思合起来就是——标签 idmodalOfTriggerViaMarkupAPI 的 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">) 上触发的,主要有四个:

  1. show.bs.modal :在 Modal 显示时触发。
  2. shown.bs.modal :在 Modal 显示之后触发。
  3. hide.bs.modal :在 Modal 隐藏时触发。
  4. hidden.bs.modal :在 Modal 隐藏之后触发。

五、设备可访问性

为了提高代码的设备可访问性——盲人借助阅读设备同样可以很好地阅读网页内容,我们会给 Modal 添加一些额外代码。

  1. .modal 上:添加 role="dialog"aria-labelledby="..."(值为 .modal-title 的 id)。
  2. .modal-dialog 上:添加 role="document"

另外,还可以给 .modal 添加 aria-describedby 内容是弹出框的描述。

六、参考链接

http://getbootstrap.com/javascript/#modals

(完)

Bootstrap 的 Modal的更多相关文章

  1. 解决select2在bootstrap的modal中默认不显示的问题

    在Bootstrap中的Modal,select2插件会有不显示,因为其z-index小于modal,还有另外一个问题是,修正z-index之后,select2不会自动失去焦点的问题.代码解决如下: ...

  2. bootstrap 模态 modal 小例子

    bootstrap 模态 modal  小例子 <html> <head> <meta charset="utf-8" /> <title ...

  3. 利用bootstrap的modal组件自定义alert,confirm和modal对话框

    由于浏览器提供的alert和confirm框体验不好,而且浏览器没有提供一个标准的以对话框的形式显示自定义HTML的弹框函数,所以很多项目都会自定义对话框组件.本篇文章介绍自己在项目中基于bootst ...

  4. bootstrap 模态 modal 小例子【转】

    bootstrap 模态 modal  小例子 <html> <head> <meta charset="utf-8" /> <title ...

  5. 【bootstrap】modal模态框的几种打开方法+问题集锦

    第一部分: 关于bootstrap中modal的使用,下面把几种自己用的打开方法展示出来 首先呢,得有个Bootstrap的页面,这里就不说了. 其次呢,得有个modal放在页面中,不管你这段代码加在 ...

  6. bootstrap的modal弹窗,在多层窗口关闭时只会关闭自窗口,不再关闭父窗口

    bootstrap多层modal弹窗时.当子窗口关闭时,所有父窗口一起关闭. 原因是bootstrap在窗口关闭事件委托时,委托给所有窗口. 如源码: this.$element.on('click. ...

  7. 学会用bootstrap的modal和carousel

    bootstrap框架提供了很多好用的javascript组件,可以很方便的实现常用的js效果,比如点击弹出一个div(modal).下拉菜单.旋转木马(carousel或slider),非常适合前端 ...

  8. 关于bootstrap的modal弹出层嵌套子Modal所引发的血案(转)

    原文地址 http://blog.csdn.net/liuxiaogangqq/article/details/51821359 bootstrap的弹出层嵌套有一个问题 ,当子modal关闭时父的m ...

  9. Bootstrap的$(...).modal is not a function错误

    使用模态对话框的时候报错了,$(...).modal is not a function 有点蒙,modal是boostrap的函数,而我已经导入了 然后在pycharm的terminal中看到了这一 ...

  10. 在BootStrap的modal中使用Select2

      bootstrap 3 $.fn.modal.Constructor.prototype.enforceFocus = function() {}; bootstrap4 $.fn.modal.C ...

随机推荐

  1. web 压力测试工具ab压力测试详解

    Web性能压力测试工具之ApacheBench(ab)详解 原文:http://www.ha97.com/4617.html PS:网站性能压力测试是性能调优过程中必不可少的一环.只有让服务器处在高压 ...

  2. 牛客网 PAT乙级(Basic Level)练习题 1023 考新郎

    题目描述 过年期间,老家举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做“考新郎”,具体的操作是这样的: 1. 首先,给每位新娘打扮得几乎一模一样,并盖上大大 ...

  3. .NET MVC 异步提交和返回参数

    一.后台页面中的接收方法和返回写法 Jsonresult意味着返回值是json格式,也可以是string或者int等其他类型. Httppost代表只接受Post方法. Mvc中返回Jsonresul ...

  4. 洛谷 P3048 [USACO12FEB]牛的IDCow IDs

    题目描述 Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, ...

  5. java shell排序

    原理图: package suanfa; public class shellInsert { public void shellInsert1(double [] sorted,int inc){ ...

  6. Message类的属性Msg所关联的消息ID

    在做C#的Message消息处理的时候,用到了消息的msg编号不知道对应的是什么事件,所以才从网上找来资料如下,在文章最后我会给出资料的出处的. WM_NULL=0x0000 WM_CREATE=0x ...

  7. PKU campus 2018 A Wife——差分约束?/dp

    题目:http://poj.openjudge.cn/campus2018/A 有正规的差分约束做法,用到矩阵转置等等. 但也有简单(?)的dp做法. 有一个结论(?):一定要么在一天一点也不选,要么 ...

  8. navicat链接远程数据库

    1.之前使用的是常规的连接方式 学习源头: https://jingyan.baidu.com/article/0aa2237573c1e688cc0d6427.html 这里的ip地址是服务器的ip ...

  9. 除了IE浏览器能识别之外,其他浏览器都不能识别的html写法

    最近写html页面的时候发现顶部边界margin-top用了定位之后,IE的跟其他浏览器不同,所以用到了把IE跟其他浏览器区分开来的写法 <!--[if !IE]> <div cla ...

  10. jQuery 实现最简单的form表单提交 Loding 功能

    <html> <head><title></title></head> <body> <form name="e ...