BootStrap的两种模态框方式
发布时间 - 2026-01-11 01:03:25 点击率:次bootstrap的弹出层

bootstrap弹出层有多种触发方式,以下是我用到的几种方式:
1.方法一:button中属性触发
注意:button中的data-target内容应该和要和弹出层中的id保持一致
data-target=”#mymodal-data”——– id=”mymodal-data”
<!--在button上绑定触发弹出层的属性-->
<button class="btn btn-primary delete" data-toggle="modal"
data-target="#mymodal-data" data-whatever="@mdo">
修改
</button>
<!-- 模态弹出窗内容 -->
<div class="modal" id="mymodal-data" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title">弹出层标题</h4>
</div>
<div class="modal-body">
<p>弹出层主体内容</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
<button type="button" class="btn btn-primary">保存</button>
</div>
</div>
</div>
</div>
结果:
2.方法二:通过js绑定
注意:将button的id和弹出层的id分别赋给 $m_btn和$modal,当$m_btn被点击后$modal弹出。
<button class="btn btn-info" type="button" id="y-modalBtnAdd" > <label >添加</label></button>
<!-- 模态弹出窗内容 -->
<div class="modal" id="y-myModalAdd" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title">弹出层标题</h4>
</div>
<div class="modal-body">
<p>通过js绑定button和弹出层触发</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
<button type="button" class="btn btn-primary">保存</button>
</div>
</div>
</div>
</div>
<!--js代码-->
<script type="text/javascript">
$(function(){
// dom加载完毕
var $m_btn = $('#y-modalBtnAdd'); //y-modalBtnAdd是button的id
var $modal = $('#y-myModalAdd'); //y-myModalAdd是弹出的遮罩层的id,通过这两个id进行绑定
$m_btn.on('click', function(){
$modal.modal({backdrop: 'static'});
});
});
</script>
结果:
3.方法三:点击表格一行,弹出弹出层
动态给tr标签加弹出的触发属性
<!--表格-->
<table class="table table-bordered " style="width: 400px">
<thead>
<tr>
<th>一</th>
<th>二</th>
<th>三</th>
</tr>
</thead>
<tbody class="tableBody">
<tr>
<td>one</td>
<td>two</td>
<td>three</td>
</tr>
<tr>
<td>four</td>
<td>five</td>
<td>six</td>
</tr>
</tbody>
</table>
<!-- 模态弹出窗内容 -->
<div class="modal" id="mymodal-data" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title">弹出层标题</h4>
</div>
<div class="modal-body">
<p>点击表格一行内容,弹出弹出层</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
<button type="button" class="btn btn-primary">保存</button>
</div>
</div>
</div>
</div>
<!--js代码-->
<script type="text/javascript">
$(function () {
$(".tableBody>tr").each(function () {
$(this).on("click",function () {
$(this).attr({"data-toggle":"modal","data-target":"#mymodal-data","data-whatever":"@mdo"});
})
});
});
</script>
结果:
bootstrap的弹出层在整个屏幕的上半部分,可以将它居中显示。(方法二可以让弹出层居中显示)
$(function(){
// dom加载完毕
var $m_btn = $('#y-modalBtnAdd'); y-modalBtnAdd是button的id
var $modal = $('#y-myModalAdd'); y-myModalAdd是弹出的遮罩层的id,通过这两个id进行绑定
// 测试 bootstrap 居中 ,bootstrap的弹出层默认是左右居中,上下则是偏上,此代码将弹出层上下也居中了,但是会抖
动一下
$modal.on('shown.bs.modal', function(){
var $this = $(this);
var $modal_dialog = $this.find('.modal-dialog');
var m_top = ( $(document).height() - $modal_dialog.height() )/2;
$modal_dialog.css({'margin': m_top + 'px auto'});
});
});
</script>
以上所述是小编给大家介绍的BootStrap的两种模态框方式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
# bootstrap
# 模态框
# 浅析BootStrap中Modal(模态框)使用心得
# bootstrap模态框消失问题的解决方法
# Bootstrap模态框调用功能实现方法
# Bootstrap模态框(modal)垂直居中的实例代码
# BootStrap日期控件在模态框中选择时间下拉菜单无效的原因及解决办法(火狐下不能点击)
# 浅析BootStrap模态框的使用(经典)
# BOOTSTRAP时间控件显示在模态框下面的bug修复
# 页面遮罩层
# 并且阻止页面body滚动。bootstrap模态框原理
# 弹出
# 绑定
# 模态
# 这两个
# 窗内
# 小编
# 加载
# 在此
# 则是
# 两种
# 给大家
# 几种
# 将它
# 所述
# 有多种
# 给我留言
# 感谢大家
# 疑问请
# 有任何
# 层中
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Internet Explorer官网直接进入 IE浏览器在线体验版网址
Android仿QQ列表左滑删除操作
海南网站制作公司有哪些,海口网是哪家的?
专业商城网站制作公司有哪些,pi商城官网是哪个?
如何快速搭建安全的FTP站点?
如何在IIS7中新建站点?详细步骤解析
Laravel如何安装Breeze扩展包_Laravel用户注册登录功能快速实现【流程】
UC浏览器如何切换小说阅读源_UC浏览器阅读源切换【方法】
大型企业网站制作流程,做网站需要注册公司吗?
用v-html解决Vue.js渲染中html标签不被解析的问题
WordPress 子目录安装中正确处理脚本路径的完整指南
Python自然语言搜索引擎项目教程_倒排索引查询优化案例
JavaScript如何实现倒计时_时间函数如何精确控制
HTML透明颜色代码怎么让下拉菜单透明_下拉菜单透明背景指南【技巧】
Laravel怎么定时执行任务_Laravel任务调度器Schedule配置与Cron设置【教程】
Laravel Livewire是什么_使用Laravel Livewire构建动态前端界面
Laravel如何实现API版本控制_Laravel API版本化路由设计策略
laravel怎么通过契约(Contracts)编程_laravel契约(Contracts)编程方法
linux写shell需要注意的问题(必看)
Laravel如何实现文件上传和存储?(本地与S3配置)
怎样使用JSON进行数据交换_它有什么限制
Windows11怎样设置电源计划_Windows11电源计划调整攻略【指南】
如何在VPS电脑上快速搭建网站?
EditPlus中的正则表达式 实战(4)
Laravel模型事件有哪些_Laravel Model Event生命周期详解
如何快速生成ASP一键建站模板并优化安全性?
ChatGPT怎么生成Excel公式_ChatGPT公式生成方法【指南】
如何安全更换建站之星模板并保留数据?
网站制作壁纸教程视频,电脑壁纸网站?
详解Android——蓝牙技术 带你实现终端间数据传输
三星网站视频制作教程下载,三星w23网页如何全屏?
Laravel Eloquent访问器与修改器是什么_Laravel Accessors & Mutators数据处理技巧
如何用VPS主机快速搭建个人网站?
javascript如何操作浏览器历史记录_怎样实现无刷新导航
Laravel怎么实现微信登录_Laravel Socialite第三方登录集成
Gemini怎么用新功能实时问答_Gemini实时问答使用【步骤】
北京的网站制作公司有哪些,哪个视频网站最好?
Laravel怎么生成二维码图片_Laravel集成Simple-QrCode扩展包与参数设置【实战】
电商网站制作价格怎么算,网上拍卖流程以及规则?
LinuxShell函数封装方法_脚本复用设计思路【教程】
Laravel项目如何进行性能优化_Laravel应用性能分析与优化技巧大全
js实现点击每个li节点,都弹出其文本值及修改
Laravel数据库迁移怎么用_Laravel Migration管理数据库结构的正确姿势
微信h5制作网站有哪些,免费微信H5页面制作工具?
php静态变量怎么调试_php静态变量作用域调试技巧【解答】
Windows驱动无法加载错误解决方法_驱动签名验证失败处理步骤
Laravel怎么实现一对多关联查询_Laravel Eloquent模型关系定义与预加载【实战】
Laravel如何生成PDF或Excel文件_Laravel文档导出工具与使用教程
Laravel怎么实现搜索高亮功能_Laravel结合Scout与Algolia全文检索【实战】
北京网站制作费用多少,建立一个公司网站的费用.有哪些部分,分别要多少钱?

