jquery - 通过单击主框外部使用 css 退出模态

标签 jquery html css

我只使用 css 创建了这个模式弹出窗口,但是我只能在单击“X”时离开它。我想添加当用户在主框外单击时离开模式的选项,即单击灰色区域。

关于如何完成它有什么建议吗?

有必要用JQuery吗? 到目前为止,代码非常简单,这就是我试图避免它的原因。

  <div ng-controller="demoCtrl">
    <a href="#openModal">Open Modal</a>
    <div id="openModal" class="modalDialog">
      <div class="content">
        <a href="#close">X</a>
        <h1>Modal Box</h1>
        <button ng-click="changeState()">Show Client</button>
        <div ng-if="client.state">{{client.name}}</div>
      </div>
    </div>
  </div>

Plunker to check the whole thing

谢谢你:)

最佳答案

您可以在包含所有窗口大小的内容后面添加一个链接,并设置与关闭按钮相同的 href:

HTML:

  <div ng-controller="demoCtrl">
    <a href="#openModal">Open Modal</a>
    <div id="openModal" class="modalDialog">
      <a class="fullSizeBlock" href="#close">&nbsp;</a>
      <div class="content">
          <a href="#close">X</a>
          <h1>Modal Box</h1>
          <button ng-click="changeState()">Show Client</button>
        <div ng-if="client.state">{{client.name}}</div>
      </div>
    </div>
  </div>

添加了 CSS(光标不是必需的,您可以保留默认链接指针):

.fullSizeBlock{
  cursor: initial;
  position: absolute;
  display: block;
  width: 100%;
  height: 100%;
}

关于jquery - 通过单击主框外部使用 css 退出模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48149213/

相关文章:

javascript - 每 'x' 页面加载量加载灯箱

html - 对齐元素的导航菜单栏问题

javascript - 数据绑定(bind)样式不适用于 if else 条件

python - 我需要一些帮助来使用 HTML 中的 CSS 或 Xpath 识别按钮

javascript - 获取数组中的表格数据内容

javascript - 使用绑定(bind)传递值

javascript - 有没有办法在浏览器中访问蓝牙连接的设备?

html - 对齐按钮和输入的右侧

jquery - Twitter 请求和 API

javascript - 在 Javascript 中,如何在父事件处理程序中防止子元素出现默认值