【发布时间】:2020-10-30 18:21:58
【问题描述】:
在 bootstrap 5 中手动调用模式的正确方法是什么?
我想在页面打开时显示模式。我试过这个:
我的模态
<div class="modal fade" id="myModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
...
...
...
</div>
</div>
</div>
我的脚本是
var myModal = document.getElementById('myModal');
document.onreadystatechange = function() {
myModal.show();
}
但在控制台日志中出现此错误:
myModal.show is not a function
at HTMLDocument.document.onreadystatechange
【问题讨论】:
-
看起来你没有 jquery -
show()是 jquery 特有的东西。 -
我使用 bootstrap v5 而不是 v4.. v5 应该是纯 javascript?
-
尝试:var myModal = new bootstrap.Modal(document.getElementById('myModal'));
-
@Symphonic 的答案是正确的,你不要发表评论,如果你想让我删除我的答案,他/她会选择你的答案和 Symphonic。