Bootstrap Modal Plugin
Modal box (Modal) is a child form covering the parent form
1. Basic use
- Through the data attribute : set the attribute data-toggle="modal" on the controller element , and set data-target="#identifier" to control which modal box is operated.
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
打开模态框
</button>
- Via JavaScript : Using this technique, a modal box with id="identifier" can be called with a simple line of JavaScript:
// 点击提交更改的同时关闭模态框
function handleSubmit() {
$('#myModal').modal('hide')
}
Example:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Bootstrap 实例 - 模态框(Modal)插件</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<!-- 按钮触发模态框 -->
<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" data-backdrop="true"
data-keyboard="true" data-show="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-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭
</button>
<button type="button" class="btn btn-primary" onclick="handleSubmit()">
提交更改
</button>
</div>
</div>
</div>
</div>
<script>
// 点击提交更改的同时关闭模态框
// function handleSubmit() {
// $('#myModal').modal('hide')
// }
$('#myModal').on('show.bs.modal', function () {
// 执行一些动作...
console.log('打开了模态框,可以执行一些操作。。。')
})
$('#myModal').on('hide.bs.modal', function () {
// 执行一些动作...
console.log('关闭了模态框,可以执行一些操作。。。')
})
</script>
</body>
</html>
2. Code interpretation
- .modal , used to
The content is recognized as a modal box.
- The second is .fade . When the modal box is switched, it will cause the content to fade in and out.
- aria-labelledby="myModalLabel" , this attribute refers to the title of the modal box.
- aria-hidden="true" is used to keep the modal window invisible until the trigger is triggered.
- **
**, modal-header is a class that defines the style for the head of the modal window.
- class="close" , close is a CSS class used to style the close button of the modal window.
- data-dismiss="modal" , used to close the modal window.
- class="modal-body" is a CSS class of Bootstrap CSS, which is used to style the body of the modal window.
- class="modal-footer" is a CSS class of Bootstrap CSS, which is used to style the bottom of the modal window.
- data-toggle="modal" , used to open the modal window.
- data-backdrop="static", when the user clicks outside the modal box, the modal box will not be closed. Remove this attribute and click outside the modal box to close the modal box
- data-keyboard="true", when it is true, the esc key can exit the modal box; when it is false, it cannot
- When data-show="false" is false, the first click to open the modal will not work; true will work
3. Event
- Open the modal box for execution
$('#myModal').on('show.bs.modal', function () {
// 执行一些动作...
console.log('打开了模态框,可以执行一些操作。。。')
})
- Close modal
$('#myModal').on('hide.bs.modal', function () {
// 执行一些动作...
console.log('关闭了模态框,可以执行一些操作。。。')
})
More reference: https://www.runoob.com/bootstrap/bootstrap-modal-plugin.html