迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

JavaScript 中的关闭模态

作者:迹忆客 最近更新:2023/03/10 浏览次数:

Bootstrap Modals 是一个可定制且响应迅速的轻量级多用途 JavaScript 弹出窗口。网站可以使用它来显示警报弹出窗口、视频和照片。

本篇文章有助于了解如何在 JavaScript 的帮助下关闭 Bootstrap 模态。

你需要提供链接或按钮来启动模态。触发器元素的标记可能如下所示。

<button id="endmodal" class="btn btn-primary close" data-dismiss="modal">
  Launch google.com
</button>

你可以使用 data-dismiss 属性来通知 Bootstrap 删除该元素。

使用单行 JavaScript,你可以调用 id 为 myModal 的模态。

语法:

$("#myModal").modal(options);

你可以通过使用在打开和关闭模态时触发的各种事件来进一步调整 Bootstrap 模态的默认行为。

语法:

$("#myBtn").on("click", function () {
  $("#myModal").modal("show");
});

这里 .modal('show') 手动打开模态。

modal 是 Bootstrap 最有用的插件之一。这是新手设计师无需编写任何 JavaScript 即可在弹出屏幕中加载材料的最简单方法之一。

例子:

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>

  <div class="container">
    <h2>Activate Modal with JavaScript</h2>
    <!-- Trigger the modal with a button -->
    <button type="button" class="btn btn-info btn-lg" id="myBtn">Open Modal</button>

    <!-- Modal -->
    <div class="modal fade" id="myModal" role="dialog">
      <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Modal Header</h4>
          </div>
          <div class="modal-body">
            <p>Some text in the modal.</p>
          </div>
          <div class="modal-footer">
            <button id="closemodal" class="btn btn-primary close" data-dismiss="modal">Launch google.com</button>
          </div>
        </div>

      </div>
    </div>

  </div>
  <script>
    $(document).ready(function () {
      $('#myModal').modal('show');

      $('#myBtn').on('click', function () {
        $('#myModal').modal('show');
      });

      $("#closemodal").on('click', function () {
        window.open("https://www.google.com", "_blank");
      });

    });
  </script>
</body>
</html>

输出:

运行代码时,你将看到如下所示的 Bootstrap 模态。

Bootstrap 模态

点击 launch google.com 按钮后,它将打开一个新链接,如下所示。

 

当你返回上一页时,你将看到单击链接后弹出窗口关闭。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便