当前位置:首页 > jquery

jquery 弹框

2026-02-03 10:23:49jquery

jQuery 弹框实现方法

使用 jQuery UI Dialog

jQuery UI 提供了一个 Dialog 组件,可以快速创建弹框。需要引入 jQuery UI 库。

<!-- 引入 jQuery 和 jQuery UI -->
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<!-- 弹框内容 -->
<div id="dialog" title="Basic dialog">
  <p>This is a dialog box.</p>
</div>

<!-- 触发按钮 -->
<button id="open-dialog">Open Dialog</button>

<script>
$( "#dialog" ).dialog({
  autoOpen: false,
  modal: true,
  buttons: {
    "OK": function() {
      $( this ).dialog( "close" );
    }
  }
});

$( "#open-dialog" ).click(function() {
  $( "#dialog" ).dialog( "open" );
});
</script>

使用 Bootstrap Modal

如果项目中使用了 Bootstrap,可以直接使用其 Modal 组件。

<!-- 引入 Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

<!-- 触发按钮 -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

<!-- 弹框内容 -->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <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 type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

自定义 jQuery 弹框

如果需要完全自定义弹框,可以通过 jQuery 操作 DOM 实现。

<style>
.custom-modal {
  display: none;
  position: fixed;
  z-index: 1;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgba(0,0,0,0.4);
}

.modal-content {
  background-color: #fefefe;
  margin: 15% auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}

.close {
  color: #aaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
  cursor: pointer;
}
</style>

<!-- 触发按钮 -->
<button id="myBtn">Open Modal</button>

<!-- 弹框内容 -->
<div id="myModal" class="custom-modal">
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Some text in the modal..</p>
  </div>
</div>

<script>
$(document).ready(function(){
  $("#myBtn").click(function(){
    $("#myModal").show();
  });

  $(".close").click(function(){
    $("#myModal").hide();
  });
});
</script>

使用第三方插件

有许多优秀的 jQuery 弹框插件可供选择,如 SweetAlert、jBox 等。

SweetAlert 示例:

<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
$("#show-alert").click(function(){
  swal("Hello world!");
});
</script>

每种方法都有其适用场景,jQuery UI Dialog 适合需要丰富功能的弹框,Bootstrap Modal 适合基于 Bootstrap 的项目,自定义弹框提供了最大的灵活性,而第三方插件则可以快速实现美观的效果。

jquery 弹框

标签: jquery弹框
分享给朋友:

相关文章

jquery下载

jquery下载

jQuery下载方法 官方下载渠道 访问jQuery官网(https://jquery.com/),点击页面中的“Download”按钮。提供两个版本选择: Production版本:压缩后的精…

jquery 之家

jquery 之家

jQuery 之家是一个专注于 jQuery 相关资源的中文网站,提供插件、教程、代码示例等内容。以下是相关信息整理: jQuery 之家网站内容 该网站通常包含以下资源: jQuery…

jquery最新版本

jquery最新版本

jQuery 最新版本 jQuery 是一个广泛使用的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。以下是关于其最新版本的信息: 当前稳定版本 jQuer…

jquery之家

jquery之家

jQuery之家相关资源 jQuery之家是一个提供jQuery插件、教程和资源的网站,适合开发者学习和使用jQuery技术。以下是关于jQuery之家的主要内容和资源: 网站内容 提供丰富的…

jquery 面试题

jquery 面试题

jQuery 面试题常见考点 DOM 操作 选择器使用:$("#id"), $(".class"), $("tag") 元素增删改查:append(), remove(), html(),…

jquery怎么读

jquery怎么读

jQuery的正确发音 jQuery的发音为 "jay-query"(/ˈdʒeɪkwɪəri/)。其中: "jay" 发音类似字母 "J" 的英文读法。 "query" 发音与英文单词 "quer…