BOOTSTRAP时间控件显示在模态框下面的bug修复

针对“BOOTSTRAP时间控件显示在模态框下面的bug修复”的问题,我提供以下完整攻略:

针对“BOOTSTRAP时间控件显示在模态框下面的bug修复”的问题,我提供以下完整攻略:

问题描述

在使用BOOTSTRAP时间控件时,当该控件被放置在模态框(Modal)中时,会出现控件被模态框遮挡,无法选取的bug。这是因为模态框Z-index值的默认设置会使得该控件表现异常。

修复步骤

要解决这个问题,我们可以通过以下步骤:

  1. 首先,需要将BOOTSTRAP时间控件的Z-index值与模态框保持一致。
.datepicker {
    z-index:1052 !important;
}
  1. 然后,可以将该CSS代码添加到一个单独的CSS文件中,例如style.css文件中,并在HTML文件中引用该CSS文件。
<link rel="stylesheet" href="style.css">

这样就能够修复该问题,使得BOOTSTRAP时间控件能够显示在Modal中。

示例说明

以下是两个示例,说明如何在HTML文件中实现以上步骤:

示例1

HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>BOOTSTRAP时间控件和模态框</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="style.css">
</head>
<body>

    <div class="container">
        <h1>BOOTSTRAP时间控件和模态框</h1>
        <button type="button" class="btn btn-primary" 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 class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="myModalLabel">模态框标题</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <input class="form-control datepicker" type="text" value="2019-09-26">
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-primary">保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

</body>
</html>

CSS

.datepicker {
    z-index: 1052 !important;
}

示例2

HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>BOOTSTRAP时间控件和模态框</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="style.css">
</head>
<body>

    <div class="container">
        <h1>BOOTSTRAP时间控件和模态框</h1>
        <button type="button" class="btn btn-primary" 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 class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="myModalLabel">模态框标题</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <input class="form-control datepicker" type="text" value="2019-09-26">
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-primary">保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <script src="script.js"></script>

</body>
</html>

CSS

.datepicker {
    z-index: 1052 !important;
}

JS

$(function() {
    $('.datepicker').datepicker();
});

本文标题为:BOOTSTRAP时间控件显示在模态框下面的bug修复

基础教程推荐