Use of JavaScript modal box in Bootstrap – interaction without leaving the parent form – click the button to pop up the dialog box

Bootstrap’s JavaScript modal is a child form that overlays the parent form. Usually, the purpose is to display a separate piece of content that can have some interaction without leaving the parent form.
Subforms can customize content and provide functions such as information display and interaction.

01-A simple, basic modal box sample code

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport" content="width=device-width,initial-scale=1, shrink-to-fit=no">
    <!--Introducing Bootstrap style sheet files-->
    <link rel="stylesheet" href="bootstrap-4.5.3-dist/css/bootstrap.css">
    <!--Introduce jQuery framework file-->
    <script src="jquery-3.5.1.slim.js"></script>
    <!--Introducing Bootstrap script files-->
    <script src="bootstrap-4.5.3-dist/js/bootstrap.min.js"></script>
</head>
<body>
<h3 align="center" >Define modal box<h3>
<a href="#myModal" class="btn btn-default" data-toggle="modal">Click here to open the modal box</a>
<div id="myModal" class="modal">
    <div class="modal-dialog">
        <div class="modal-content">
            <h3>Modal box</h3>
            <p>This is the content of the pop-up modal box</p>
        </div>
    </div>
</div>
</body>
</html>

The running effect is as follows:

02-A complete modal box sample code

A complete modal box includes three parts: header, body and footer, which are defined using the classes modal-header, modal-body and class modal-footer respectively.
The sample code is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>A complete modal box sample code</title>
    <meta name="viewport" content="width=device-width,initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="bootstrap-4.5.3-dist/css/bootstrap.css">
    <script src="jquery-3.5.1.slim.js"></script>
    <script src="bootstrap-4.5.3-dist/js/bootstrap.min.js"></script>
</head>
<body class="container">
<h3 align="center">A complete modal box sample code</h3>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#Modal">
    Open modal box
</button>
<div class="modal fade" id="Modal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="modalTitle">In early spring, Zhang Shiba of the Ministry of Water Resources was presented to the outside</h5>
                <button type="button" class="close" data-dismiss="modal">
                    <span> &times;</span>
                </button>
            </div>
            <div class="modal-body">The light rain on Tianjie is as moist as crisp, and the color of grass looks far away but not up close. </div>
            <div class="modal-body">The best thing about spring is that it beats the smoke and willows all over the imperial capital. </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">More</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>


Key code description:

data-dismiss="modal"< in code

syntaxbug.com © 2021 All Rights Reserved.