Bootstrap 模态框怎么用? 下面本篇文章给大家介绍一下 Bootstrap 模态框的使用. 有一定的参考价值, 有需要的朋友可以参考一下, 希望对大家有所帮助.
Bootstrap 的模态框
如果只想单独使用模态框功能, 可以单独引入 modal.JS, 和 Bootstrap 的 CSS, 在 Bootstrap 的包中, 可引入 Bootstrap.JS.
用法
通过 data 属性, 比如设置某个 button 的 data-toggle="modal", 同时设置 data-target="#myModal" 选择器内容,
通过 JS 直接用代码 $('#myModal').modal(options)
模态框主要为三部分, model-head,modeal-body,model-footer, 主要内容在 body 中显示, class="close" 为一个关闭模态框样式.
几个常用的方法 $('#identifier').modal('toggle') 切换模态框状态
$('#identifier').modal('show') 显示模态框
$('#identifier').modal('hide') 隐藏模态框
事件 | 作用 | 用法 |
---|---|---|
show.bs.modal | 在调用 show 方法后触发。 | $(’#myModal’).on(‘show.bs.modal’,function () {alert(“显示模态框”);}); |
shown.bs.modal | 在调用 show 方法后触发。 | $(’#myModal’).on(‘shown.bs.modal’, function () {alert(“完全显示模态框”); }); |
hide.bs.modal | 在调用 hide 方法后触发。 | $(’#myModal’).on(‘hide.bs.modal’, function () {alert(“隐藏模态框”);}); |
hidden.bs.modal | 在调用 hide 方法后触发。 | $(’#myModal’).on(‘hidden.bs.modal’, function () {alert(“完全隐藏模态框”); }); |
实例代码
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
- <%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
- <!DOCTYPE HTML>
- <HTML>
- <head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
- <title>shiro</title>
- <script type="text/javascript" src="js/jquery-3.3.1.js"></script>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
- <script src="https://cdn.bootcss.com/jquery/3.4.1/core.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
- <script type="text/javascript">
- var basePath = "${pageContext.request.contextPath}";
- </script>
- </head>
- <body>
- <div>
- <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
开始演示模态框
</button> <!-- 模态框(Modal) --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div> <div> <div> <button type="button" data-dismiss="modal" aria-hidden="true"> × </button> <h4 id="myModalLabel">
模态框 (Modal) 标题
</h4> </div> <div> <div > 我是模态框</div> </div> <div> <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> </div> </body> <script type="text/javascript"> $('#myModal').on('show.bs.modal', function () { alert("显示模态框"); }); $('#myModal').on('shown.bs.modal', function () { alert("完全显示模态框"); }); $('#myModal').on('hide.bs.modal', function () { alert("隐藏模态框"); }); $('#myModal').on('hidden.bs.modal', function () { alert("完全隐藏模态框"); }); </script> </HTML>
更多 Bootstrap 的相关知识, 可访问: web 前端自学 https://www.html.cn/ !!
来源: http://www.css88.com/framework/bootstrap/18003.html