bootstrap模态框手动开启关闭与设置点击外部不关闭
完整的参考菜鸟教程:http://www.runoob.com/bootstrap/bootstrap-modal-plugin.html
1.手动开启与关闭模态框的方法 按钮开启与JS函数开启(2种)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Bootstrap 实例 - 模态框(Modal)插件</title> <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <h2>创建模态框(Modal)</h2> <!-- 按钮触发模态框 --> <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal1"> 按钮开启 </button> <a href="javascript:void(0)" class="btn btn-default" onclick="openMotai()">JS函数开启</a> <script type="text/javascript"> function openMotai(){ alert("准备开启"); $("#myModal1").modal('show'); //手动开启 alert("准备关闭"); $("#myModal1").modal('hide'); //手动关闭 alert("准备开启"); $("#myModal1").modal(); //手动开启 } </script> <!-- 模态框(Modal) --> <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header">
<!--关闭符号-->
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"> × </button> <h4 class="modal-title" id="myModalLabel"> 模态框(Modal)标题 </h4> </div> <div class="modal-body"> username:<input type="text" name="" id="" value="" /><br /> password: <input type="password" name="" /> </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> <!-- /.modal-content --> </div> <!-- /.modal --> </div> </body> </html>
2.有时候我们希望点击esc或者点击模态框外部的时候不会关闭
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Bootstrap 实例 - 模态框(Modal)插件</title> <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <h2>创建模态框(Modal)</h2> <!-- 按钮触发模态框 --> <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal1"> 按钮开启 </button> <a href="javascript:void(0)" class="btn btn-default" onclick="openMotai()">JS函数开启</a> <script type="text/javascript"> function openMotai(){ /*alert("准备开启"); $("#myModal1").modal('show'); //手动开启 alert("准备关闭"); $("#myModal1").modal('hide'); //手动关闭 alert("准备开启"); $("#myModal1").modal(); //手动开启*/ $("#myModal1").modal({backdrop: 'static', keyboard: false}); //手动开启 } </script> <!-- 模态框(Modal) --> <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header">
<!-- 关闭符号(叉号) -->
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"> × </button> <h4 class="modal-title" id="myModalLabel"> 模态框(Modal)标题 </h4> </div> <div class="modal-body"> username:<input type="text" name="" id="" value="" /><br /> password: <input type="password" name="" /> </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> <!-- /.modal-content --> </div> <!-- /.modal --> </div> </body> </html>
分析:
$(‘#myModal’).modal({backdrop: ‘static’, keyboard: false});
其中 ,backdrop:’static’指的是点击背景空白处不被关闭;
keyboard:false指的是触发键盘esc事件时不关闭。
另一种方式是直接在模态框标签加::
<!-- 模态框黑名单单位添加-->
<div class="modal fade" id="myModal" role="dialog"
data-backdrop="static" data-keyboard="false" aria-labelledby="myModalLabel" aria-hidden="true">
【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了