bootstrap 5 关闭模态与香草 javascript

noo*_*392 21 javascript bootstrap-modal bootstrap-5

您好按照此处的教程 https://getbootstrap.com/docs/5.0/components/modal/#via-javascript ,他们使用按钮来切换显示模式。如果你想用 javascript 在 Bootstrap 5 中显示模式,你基本上可以使用 var myModal = new bootstrap.Modal(document.getElementById('staticBackdrop')); myModal.show();

我的问题是如何使用 javascript 关闭通过按钮打开的模式。基本上我怎样才能获得已经打开的模式的句柄?我意识到一旦我有了句柄我就会调用 .hide()

---编辑--- 为了弄清楚这一点。在 Bootstrap 5 中,当它不使用 javascript 打开时

<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
  Launch demo modal
</button>
Run Code Online (Sandbox Code Playgroud)

我试图掌握该模式

noo*_*392 69

弄清楚了。

var myModalEl = document.getElementById('staticBackdrop');
var modal = bootstrap.Modal.getInstance(myModalEl)
modal.hide();
Run Code Online (Sandbox Code Playgroud)

我缺少 bootstrap.Modal.getInstance。每次我都会花5个小时盯着某件事并寻找答案。我终于在这里发帖了,立刻就明白了,哈哈。

  • 这适用于 bootstrap 5.2 (5认同)