多个按钮触发同一个Bootstrap自适应模态窗口
在项目中可能会面对这样的一个场景:
界面上有多个按钮,我们希望点击这些按钮弹出同一个模态窗口,但希望模态窗口的内容是动态生成的,即,点击每个按钮弹出的模态窗口内容不同。
通常情况下,一个按钮对应一个模态窗口。
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title><link href="css/bootstrap.min.css" rel="stylesheet" /><script src="Scripts/jquery-2.1.1.min.js"></script><script src="js/bootstrap.min.js"></script><style type="text/css">body.modal-open,.modal-open .navbar-fixed-top,.modal-open .navbar-fixed-bottom {margin-right: 0;}.modal {top: 100px;bottom: auto;padding: 0;background-color: #ffffff;border: 1px solid #999999;border: 1px solid rgba(0, 0, 0, 0.2);border-radius: 6px;-webkit-box-shadow: 0 3px 9px rgba(0, 0, 0, 0.5);box-shadow: 0 3px 9px rgba(0, 0, 0, 0.5);background-clip: padding-box;}.modal.container {max-width: none;}</style></head><body><div class="content" style="margin-left: 100px;margin-top: 100px;"><button class="btn btn-primary btn-lg" data-toggle="modal" href="#full-width">打开模态窗口</button></div><div id="full-width" class="modal container fade" tabindex="-1" style="display: none;"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h4 class="modal-title">标题</h4></div><div class="modal-body"><p>主体内容主体内容主体内容主体内容主体内容主体内容主体内容主体内容主体内容主体内容主体内容主体内容主体内容主体内容主体内容</p></div><div class="modal-footer" style="text-align: center;"><button type="button" data-dismiss="modal" class="btn btn-default">关闭</button></div></div></body>
效果如下:
以上,通过data-toggle="modal" href="#full-width"实现模态窗口。
现在,在页面上存在2个按钮:
<button class="btn btn-primary btn-lg">打开模态窗口1</button><button class="btn btn-primary btn-lg">打开模态窗口2</button>
我们希望点击每个按钮都弹出id为full-width的模态窗口,但模态窗口的标题为按钮的文本。
于是,需要通过Javascript的API来弹出模态窗口,并且,在弹出之前需要把按钮的文本赋值给模态窗口的标题。
$(function() {$('.content').on("click", "button", function () {$('#full-width .modal-header .modal-title').empty().text($(this).text());$('#full-width').modal();});});
完整如下:
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title><link href="css/bootstrap.min.css" rel="stylesheet" /><script src="Scripts/jquery-2.1.1.min.js"></script><script src="js/bootstrap.min.js"></script><style type="text/css">body.modal-open,.modal-open .navbar-fixed-top,.modal-open .navbar-fixed-bottom {margin-right: 0;}.modal {top: 100px;bottom: auto;padding: 0;background-color: #ffffff;border: 1px solid #999999;border: 1px solid rgba(0, 0, 0, 0.2);border-radius: 6px;-webkit-box-shadow: 0 3px 9px rgba(0, 0, 0, 0.5);box-shadow: 0 3px 9px rgba(0, 0, 0, 0.5);background-clip: padding-box;}.modal.container {max-width: none;}</style><script type="text/javascript">$(function() {$('.content').on("click", "button", function () {$('#full-width .modal-header .modal-title').empty().text($(this).text());$('#full-width').modal();});});</script></head><body><div class="content" style="margin-left: 100px;margin-top: 100px;"><button class="btn btn-primary btn-lg">打开模态窗口1</button><button class="btn btn-primary btn-lg">打开模态窗口2</button></div><div id="full-width" class="modal container fade" tabindex="-1" style="display: none;"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h4 class="modal-title">标题</h4></div><div class="modal-body"><p>主体内容主体内容主体内容主体内容主体内容主体内容主体内容主体内容主体内容主体内容主体内容主体内容主体内容主体内容主体内容</p></div><div class="modal-footer" style="text-align: center;"><button type="button" data-dismiss="modal" class="btn btn-default">关闭</button></div></div></body>
效果如下:

多个按钮触发同一个Bootstrap自适应模态窗口的更多相关文章
- 使用jQuery和Bootstrap实现多层、自适应模态窗口
本篇实践一个多层模态窗口,而且是自适应的. 点击页面上的一个按钮,弹出第一层自适应模态窗口. 在第一层模态窗口内包含一个按钮,点击该按钮弹出第二层模态窗口,弹出的第二层模态窗口会挡住第一层模态窗口,即 ...
- bootstrap 自定义模态窗口
$(".classname").click(function () { $('#mymodel').modal('show'); alert('模态框打开了'); }); $('# ...
- BootStrap入门教程 (四) :JQuery类库插件(模态窗口,滚动监控,标签效果,提示效果,“泡芙”效果,警告区域,折叠效果,旋转木马,输入提示)
上讲回顾:Bootstrap组件丰富同时具有良好可扩展性,能够很好地应用在生产环境.这些组件包括按钮(Button),导航(Navigation),缩略图( thumbnails),提醒(Alert) ...
- bootstrap 模态窗口 多重/多个弹窗滚动条补丁
由于bootstrap的模态窗口默认不支持多次弹出, 在关闭的时候会有滚动条消失的问题. 经过观察和查看源码, 发现在开启和关闭的时候会在body上增加/减少一个"modal-open&qu ...
- WebUploader 上传插件结合bootstrap的模态框使用时选择上传文件按钮无效问题的解决方法
由于种种原因(工作忙,要锻炼健身,要看书,要学习其他兴趣爱好,谈恋爱等),博客已经好久没有更新,为这个内心一直感觉很愧疚,今天开始决定继续更新博客,每周至少一篇,最多不限篇幅. 今天说一下,下午在工作 ...
- Bootstrap 实例 - 模态框(Modal)插件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- bootstrap的模态框
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- layui实现类似于bootstrap的模态框功能
以前习惯了bootstrap的模态框,突然换了layui,想的用layui实现类似于bootstrap的模态框功能. 用到了layui的layer模块,例如: <!DOCTYPE html> ...
- Bootstrap使用模态框modal实现表单提交弹出框
Bootstrap 模态框(Modal)插件 模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等.如果 ...
随机推荐
- Linux内核启动流程分析(二)【转】
转自:http://blog.chinaunix.net/uid-25909619-id-3380544.html S3C2410 Linux 2.6.35.7启动分析(第二阶段) 接着上面的分析,第 ...
- Ansible Tower系列 二(安装 Tower)【转】
文档:http://docs.ansible.com/ansible-tower/ 安装前检查 python版本为2.6 保持网络畅通 内存预留充足 安装用户为root 软件下载 下载地址:http: ...
- MAC系统下Sublime Text3 配置Python3详细教程
MAC系统下Sublime Text3 配置Python3详细教程(亲测有效) https://blog.csdn.net/weixin_41768008/article/details/798590 ...
- 在Docker中运行EOS(MAC版)
在Docker中运行EOS(MAC版) 在Docker中也可以简单快速的构建EOS.IO.笔者在Mac平台下参考官方文档躺了一次河.记录如下: 安装依赖 Docker 版本 17.05或者更高 tes ...
- @PostConstruct和@PreConstruct
详情参见:https://www.cnblogs.com/landiljy/p/5764515.html 1.@PostConstruct说明 被@PostConstruct修饰的方法会在服务器加载S ...
- spring boot JPA中实体类常用注解
spring boot jpa中的注解很多,参数也比较多.没必要全部记住,但是经常查看官方文档也比较麻烦,记录一下一些常用的注解.通过一些具体的例子来帮助记忆. @Entity @Table(name ...
- jquery-事件之页面框架加载后自动执行
jQuery事件之页面框架加载后自动执行 1)概述 HTML执行是按自上而下编译,而<script>一般写在body结束之前.如果在HTML加载的过程中卡住, 比如加载图片等,没有显示出来 ...
- jquery-实用例子
一:jquery实现全选取消反选 3元运算:条件?真值:假值 <!DOCTYPE html> <html lang="en"> <head> & ...
- 目标检测--Selective Search for Object Recognition(IJCV, 2013)
Selective Search for Object Recognition 作者: J. R. R. Uijlings, K. E. A. van de Sande, T. Gevers, A. ...
- 直接以管理员身份运行bat代码
https://www.cnblogs.com/dengpeng1004/p/7044441.html