Bootstrap 5 - 页面加载时打开模式
Posted
技术标签:
【中文标题】Bootstrap 5 - 页面加载时打开模式【英文标题】:Bootstrap 5 - Modal Open on Page Load 【发布时间】:2021-11-30 16:42:32 【问题描述】:我正在尝试实现一个在为学校作业加载页面时自动打开的模式。我正在使用 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" 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>
如果有人知道问题出在哪里或如何解决,我们将不胜感激。
【问题讨论】:
getbootstrap.com/docs/5.1/components/modal/#via-javascript 【参考方案1】:首先欢迎来到 *** 社区。p>
正如您所问的,您首先更正了模式对话框代码 sn-p。你错过了那里的第一行模式。您必须将 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
导入到您的代码中。否则代码 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 5 - 页面加载时打开模式的主要内容,如果未能解决你的问题,请参考以下文章