我需要覆盖显示在第一个模态上面,而不是后面.

Modal overlay behind

$('#openBtn').click(function(){
	$('#myModal').modal({show:true})
});
<a data-toggle="modal" href="#myModal" class="btn btn-primary">Launch modal</a>

<div class="modal" id="myModal">
	<div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h4 class="modal-title">Modal title</h4>
        </div><div class="container"></div>
        <div class="modal-body">
          Content for the dialog / modal goes here.
          <br>
          <br>
          <br>
          <br>
          <br>
          <a data-toggle="modal" href="#myModal2" class="btn btn-primary">Launch modal</a>
        </div>
        <div class="modal-footer">
          <a href="#" data-dismiss="modal" class="btn">Close</a>
          <a href="#" class="btn btn-primary">Save changes</a>
        </div>
      </div>
    </div>
</div>
<div class="modal" id="myModal2" data-backdrop="static">
	<div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h4 class="modal-title">Second Modal title</h4>
        </div><div class="container"></div>
        <div class="modal-body">
          Content for the dialog / modal goes here.
        </div>
        <div class="modal-footer">
          <a href="#" data-dismiss="modal" class="btn">Close</a>
          <a href="#" class="btn btn-primary">Save changes</a>
        </div>
      </div>
    </div>
</div>


<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/js/bootstrap.min.js"></script>

我试图改变.modal-backdrop中的z-index,但它变得一团糟.

在某些情况下,我在同一页上有两个以上的情态动词.

推荐答案

Solution inspired by the answers of @YermoLamers & @Ketwaroo.

Backdrop z-index fix
This solution uses a setTimeout because the .modal-backdrop isn't created when the event show.bs.modal is triggered.

$(document).on('show.bs.modal', '.modal', function() {
  const zIndex = 1040 + 10 * $('.modal:visible').length;
  $(this).css('z-index', zIndex);
  setTimeout(() => $('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack'));
});
  • 这适用于在页面上创建的每.modal个(即使是动态模态)
  • 背景立即覆盖上一个模态

示例jsfiddle

z-index
If you don't like the hardcoded z-index for any reason you can calculate the highest z-index on the page like this:

const zIndex = 10 +
  Math.max(...Array.from(document.querySelectorAll('*')).map((el) => +el.style.zIndex));

Scrollbar fix
If you have a modal on your page that exceeds the browser height, then you can't scroll in it when closing an second modal. To fix this add:

$(document).on('hidden.bs.modal', '.modal',
  () => $('.modal:visible').length && $(document.body).addClass('modal-open'));

Versions
This solution is tested with bootstrap 3.1.0 - 3.3.5

Javascript相关问答推荐

nPM审计始终发现0个漏洞

仅圆角的甜甜圈图表

django无法解析余数:[0] from carray[0]'

yarn安装一个本地npm包,以便本地包使用main项目的node_modules(ckeditor-duplicated-modules错误)

Plotly热图:在矩形上zoom 后将zoom 区域居中

无法在nextjs应用程序中通过id从mongoDB删除'

配置WebAssembly/Emscripten本地生成问题

数字时钟在JavaScript中不动态更新

查找最长的子序列-无法重置数组

这个值总是返回未定义的-Reaction

从页面到应用程序(NextJS):REST.STATUS不是一个函数

第三方包不需要NODE_MODULES文件夹就可以工作吗?

如何利用CSS中的隐藏元素实现平滑扩展和防止网格行间隙

在不扭曲纹理的情况下在顶点着色器中旋转UV

将Singleton实例设置为未定义后的Angular 变量引用持久性行为

如何在Reaction中清除输入字段

在单击按钮时生成多个表单时的处理状态

如何检测当前是否没有按下键盘上的键?

是否有静态版本的`instanceof`?

在点击链接后重定向至url之前暂停