我已经直接从Bootstrap示例中使用了我的模态代码,并且只包括Bootstrap .js(而不是Bootstrap -modal.js)。但是,我的模态出现在灰色渐隐(背景)下面,是不可编辑的。

这是它的样子:

这是重现这个问题的一种方法。该代码的基本结构是这样的:

<body>
    <p>Lorem ipsum dolor sit amet.</p>    

    <div class="my-module">
        This container contains the modal code.
        <div class="modal fade">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-body">Modal</div>
                </div>
            </div>
        </div>
    </div>
</body>
body {
    padding-top: 50px;
}

.my-module {
    position: fixed;
    top: 0;
    left: 0;
}

你知道为什么会这样吗或者我能做些什么来弥补吗?


当前回答

这个问题与父容器的定位有关。你可以很容易地将你的模式从这些容器中“移动”出来,然后再显示出来。下面是如何做到这一点,如果你显示你的模式使用js:

$('#myModal').appendTo("body").modal('show');

或者,如果你使用按钮启动模式,删除.modal('show');只要这样做:

$('#myModal').appendTo("body") 

这将保持所有正常的功能,允许您使用按钮显示模式。

其他回答

只需添加两行CSS:

.modal-backdrop{z-index: 1050;}
.modal{z-index: 1060;}

modal- background应该有1050的值来设置它在导航栏上。

在你的导航栏navbar-fixed-top需要z-index = 1041 如果你使用bootstarp-combined.min.css,也需要将。navbar-fixed-top, .navbar-fixed-bottom z-index改为1041

我找到的最简单的解决方案,对我所有的案例都有效,就是在类似的帖子中回答:

下面就是:

.modal {
  background: rgba(0, 0, 0, 0.5); 
}
.modal-backdrop {
  display: none;
}

基本上,原来的背景没有使用。相反,你使用情态动词作为背景。结果是它的外观和行为与原始版本完全一致。它避免了z索引的所有问题(对我来说,将z索引改为0解决了这个问题,但创建了一个新的问题,我的导航菜单介于模式和背景之间),而且很简单。

感谢@Jevin O. Sewaruth发布的原始答案。

上述建议的解决方案都不适合我,但这个技巧解决了问题:

$('#myModal').on('shown.bs.modal', function() {
   //To relate the z-index make sure backdrop and modal are siblings
   $(this).before($('.modal-backdrop'));
   //Now set z-index of modal greater than backdrop
   $(this).css("z-index", parseInt($('.modal-backdrop').css('z-index')) + 1);
}); 

对我来说,最简单的解决方案是把我的模态放在一个绝对位置和z索引高于.modal- background的包装器中。因为模态背景(至少在我的情况下)的z指数为1050:

.my-modal-wrapper { 位置:绝对的; z - index: 1060; } < div class = " my-modal-wrapper " > < / div >