Jquery 实现弹出框以外的地方关闭弹出框

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>jq点击自身以外地方关闭弹出窗</title>
  <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
  <style>
    * {
      margin: 0;
      padding: 0
    }

    #branch {
      width: 100%;
      height: auto;
    }

    /* 按钮 */
    #branch .branch-header button {
      margin: 300px 600px;
    }

    /* 弹出框 */
    #branch .branch-proup {
      display: none;
      position: fixed;
      top: 0;
      left: 0;
      z-index: 10;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, .2);
      border-radius: 5px 5px 0 0;
    }

    #branch .branch-proup-content {
      position: fixed;
      top: 50%;
      left: 50%;
      width: 320px;
      height: 320px;
      border: 1px solid #ccc;
      margin-top: -160px;
      margin-left: -160px;
      background-color: #fff;
      border-radius: 5px;
    }
  </style>
</head>

<body>
  <div id="branch">
    <div class="branch-header">
      <button>点击出现弹出框</button>
    </div>
    <!-- 弹出框 -->
    <div class="branch-proup">
      <div class="branch-proup-content">
        <!-- 弹出框内容 -->
      </div>
    </div>
  </div>
  <script type="text/javascript">
    $(function() {
      // 点击按钮
      var btn = $('#branch .branch-header button');
      // 弹出框
      var proup = $('#branch .branch-proup');

      // 点击按钮 弹窗出现
      btn.click(function() {
        proup.show();
      })

      // 点击弹窗内容以外的地方关闭弹窗
      proup.on('click', function(e) {
        if ($(e.target).closest('.branch-proup-content').length > 0) {
          alert('弹出框内部被点击了');
        } else {
          alert('弹出框以外的部分被点击了');
          // 关闭弹框
          proup.hide();
        }
      });
    })
  </script>
</body>
</html>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
jQuery UI 的框中,你可以自定义框的内容,包括 HTML 结构和样式。你可以通过以下步骤来实现: 1. 创建一个 HTML 元素来作为框的容器,并设置一个唯一的 ID: ```html <div id="myDialog" title="框标题"></div> ``` 2. 使用 jQuery UI 中的 `dialog()` 方法初始化框,并设置相关选项。其中,`autoOpen` 设置为 `false` 可以让框初始化时不自动打开: ```javascript $("#myDialog").dialog({ autoOpen: false, modal: true, // 设置为模态对话框 buttons: { "确认": function() { // 点击确认按钮后的回调函数 }, "取消": function() { $(this).dialog("close"); // 关闭框 } } }); ``` 3. 使用 jQuery 的方法来添加自定义的 HTML 内容到框中。例如,使用 `html()` 方法来设置框的内容: ```javascript $("#myDialog").html("<p>这是自定义的框内容。</p><input type='text' name='myInput'>"); ``` 在上述示例中,使用了 `<p>` 元素和一个 `<input>` 元素作为自定义内容。 4. 当需要框时,可以调用 `dialog("open")` 方法来打开框: ```javascript $("#myDialog").dialog("open"); ``` 通过以上步骤,你可以在 jQuery UI 框中自定义 HTML 内容。你可以根据需要添加任意数量和类型的 HTML 元素,并使用 CSS 来自定义样式。请注意,具体的实现可能因你的需求和使用的 jQuery UI 版本而有所不同,以上提供了一种通用的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值