Bootstrap 5 - 页面加载时模式打开

Bootstrap 5 - Modal Open on Page Load

我正在尝试实现一个模式,该模式会在为学校作业加载页面时自动打开。我正在使用 Bootstrap 5 并且我在网上找到的所有示例都使用旧版本。可以在评论 Vertically Centered Cookies Modal 下找到模态框。我使用了 Bootstrap CDN 以及来自 https://getbootstrap.com/ 的 Popper Bundle。这是我的代码:

<!DOCTYPE HTML>
<html lang="en-AU">
<head>
    <!-- Require Meta Tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        
    <!-- CDN - Bootstrap CSS -->
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    
    <!-- Title -->
        <title>Title Here</title>
    
    <!-- Webpage Icon -->
        <link rel="shortcut icon" href="Images\Pilot640_Logo_Symbol.ico"/>
        

</head>

<body>      

<!-- Nav Bar -->
<nav class="navbar navbar-expand-sm bg-secondary navbar-white">
    <div class="container-fluid">
        <a class="navbar-brand" href="#">
        <img src="Images\Pilot640_Logo_Symbol.ico" alt="Logo" style="width:100px;" class="rounded-pill">
        </a>
    </div>
</nav>

<!-- Header Section -->
<header> 

</header> 

<!-- Vertically Centered Cookies Modal -->

<div class="modal-dialog modal-dialog-centered" id="onload">
    <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Do You Want Cookie? We Want Yours! </h5>
        </div>
        <div class="modal-body">
            This site uses cookies to personalies the content for you.
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        </div>
    </div>
</div>


<!-- Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    
<!--Modal JS Script --> 
<script type="text/javascript">
    $(window).load(function(){
        $('#onload').modal('show');
    });
</script>
    
</body>
</html>

如果有人知道问题出在哪里或如何解决,将不胜感激。

首先欢迎来到 Whosebug 社区。

正如您所问,您首先更正了模态对话框代码片段。你错过了那里的第一行模态。您必须将 id 添加到该行,而不是 modal-dialog div

<div class="modal fade" id="onload" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true"> <!-- Add this line to your code -->
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Do You Want Cookie? We Want Yours! </h5>
            </div>
            <div class="modal-body">
                This site uses cookies to personalies the content for you.
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div> <!-- And the relavant closing div tag -->

然后您需要将 jquery 导入到您的代码中。否则 code jquery 代码段将无法在您的 html 文件中工作。最后,不要使用 jquery onload 事件监听器,而是使用原生 VanillaJS window.onload 事件来触发你的模式。

<!-- import jquery -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<!-- Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<!--Modal JS Script -->
<script type="text/javascript">
    window.onload = () => {
        $('#onload').modal('show');
    }
</script>

那么您的 bootstrap 模式将按照预期的行为工作。