Bootstrap 5 模式未关闭

发布于 2025-01-11 00:16:23 字数 441 浏览 0 评论 0原文

我一定是在做一些愚蠢的事情。

我正在使用 $("#exampleModal").show(); 以编程方式打开 modal 模态

打开正确。 但由于某种原因 - 我没有得到模态后面的深色叠加层 - 并且 2 个关闭按钮不起作用。

我在示例中将“fade”类应用为 ,但这意味着模式根本无法打开。

JSFIDDLE: https://jsfiddle.net/kneidels/x9zwfumt/

I must be doing something silly.

I am programmatically opening a modal using $("#exampleModal").show();

The modal opens correctly.
But for some reason - i do not get the dark overlay behind the modal - and the 2 close buttons do not work.

I had the class 'fade' applied as in the sample, but it meant the modal would not open at all.

JSFIDDLE: https://jsfiddle.net/kneidels/x9zwfumt/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静水深流 2025-01-18 00:16:23

Bootstrap 5:

$("#exampleModal").show(); 更改为 $("#exampleModal").modal("show");

它解决了你提到的一切。

请参阅下面的片段。

$(function() {

  $(".triggerlist").click(function() {
    $("#exampleModal").modal("show");
  })

});
body {
  font-size: 1.0rem;
  padding: 20px;
}

.triggerlist {
  text-decoration: underline;
  color: blue;
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js" integrity="sha512-aVKKRRi/Q/YV+4mjoKBsE4x3H+BkegoM/em46NNlCqNTmUYADjBbeNefNxYV7giUp0VxICtqdrbqU7iVaeZNXA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>

<h1>Hello, world!</h1>

<p class="triggerlist">The packages will be sent to<span class="d-none d-lg-inline">:</span> </p>

<div class="modal " id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="false">

  <div class="modal-dialog modal-dialog-scrollable">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Package Recipients</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        TEST CONTENT
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

Bootstrap 5:

Change $("#exampleModal").show(); to $("#exampleModal").modal("show");.

It solves everything you mentioned.

See the snippet below.

$(function() {

  $(".triggerlist").click(function() {
    $("#exampleModal").modal("show");
  })

});
body {
  font-size: 1.0rem;
  padding: 20px;
}

.triggerlist {
  text-decoration: underline;
  color: blue;
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js" integrity="sha512-aVKKRRi/Q/YV+4mjoKBsE4x3H+BkegoM/em46NNlCqNTmUYADjBbeNefNxYV7giUp0VxICtqdrbqU7iVaeZNXA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>

<h1>Hello, world!</h1>

<p class="triggerlist">The packages will be sent to<span class="d-none d-lg-inline">:</span> </p>

<div class="modal " id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="false">

  <div class="modal-dialog modal-dialog-scrollable">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Package Recipients</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        TEST CONTENT
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文