我正在尝试实现一个模态,当页面加载到学校作业时自动打开。我使用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>
如果有人知道问题是什么或者如何制造一个,帮助将会非常感激。
2条答案
按热度按时间js4nwp541#
首先欢迎来到Stackoverflow社区。
正如您所要求的,您已经首先更正了模态对话框代码片段。您已经错过了模态的第一行。您必须将id添加到该行,而不是添加到
modal-dialog
div然后,你需要导入
jquery
到你的代码中,否则你的html文件中的jquery代码段将无法工作,最后,不要使用jqueryonload
事件列表器,而是使用本地VanillaJSwindow.onload
事件来触发你的模态。然后,引导模式将与预期行为一起工作。
mspsb9vt2#
香草JS溶液
HTML将保持不变,但您不需要加载jQuery