javascript - 如何通过单击第一页中的按钮来激活 Bootstrap 4 模态,将您重定向到激活模态的第二页?
问题描述
对于我的项目,我想实现一个modal。我正在使用Bootstrap 4。模态应按以下方式工作:
- 当我在第一个 .html 页面中按下按钮时,该按钮应该将我重定向到第二个 .html 页面,同时在第二个 .html 页面中打开一个模式作为欢迎。
因此,这种情况下的模态就像弹出框一样的欢迎消息。
把事情说清楚。按钮“代码”在第一个 .html 文件中,模式“代码”在第二个 .html 文件中。
按钮:
<a href="index.html"><button class="btn btn-primary btn-lg btn-block text-uppercase" type="button"
data-toggle="modal" data-target="#modalLogin">Prijavi se</button></a>
模态:
<div class="modal fade" id="modalLogin" tabindex="-1" role="dialog" aria-labelledby="modalLoginLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="modalLoginLabel">Pozdravljeni!</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Dobrodošli v digitalnem gradbenem delovnem listu.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Nadaljuj</button>
</div>
</div>
</div>
</div>
我什么都试过了。从 id 到尝试重新定位代码,再到尝试使用 JavaScript 找到另一个解决方案,但到目前为止还没有运气。
解决方案
单击您<a>
无法在即将打开的另一个页面中打开模式。相反,您所做的是稍微修改您的 href <a>
,例如:<a href="index.html/welcome"></a>
.
然后,在您的 JS(正在加载index.html
)中,当用户导航到该页面时,您可以检查您的 URL 中是否有“欢迎”。如果有,则触发模态显示。它看起来像这样:
$(document).ready(function() {
if (window.location.href.indexOf("welcome") > -1) {
$("#modalLogin").modal("show");
}
});
这样,当用户导航到index.html
模态框时不会显示,但是如果用户单击您的按钮,则会显示模态框,因为 URL 将包含welcome
.
推荐阅读
- google-apps-script - 我的自我调用解决方法有效还是巧合?
- c++ - pointer_traits typedefs 可以未定义吗?
- c++ - unique_ptr 作为通用处置对象
- aggregate - 拆分不连贯的聚集体
- php - 是否可以使用 php 从 parseplaform 从 GeoPoint 获取纬度和经度
- php - 将网站页面从域重定向到子域
- c++ - GNU MPFR 在精度高于 64 时给我完全相同的结果
- performance - 是否有任何已建立的带有时间分析的 spark-sql 表达式的性能基准?
- mysql - 如何在客户端列中每个唯一值的描述中创建获取 3 个值
- firebase - Firebase Firestore - 在 React Native 中显示查询的数据