我已经直接从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;
}

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


当前回答

在我的情况下,解决方案是简单的,我忘记在div上添加id“my-modal”的“modal”类,由$('#my-modal').modal('show')提升;

因为div的内容包含了所有需要的内容,所以除了上面的覆盖之外,屏幕上的结果几乎是完美的。

我敢打赌,其他与z-index技巧或其他css技巧无关的父div或未关闭div的位置的答案也可能是在这种情况下。

其他回答

对于那些像我这样不使用插件或主题就添加Bootstrap到Wordpress的人:

我在function.php文件中添加了引导CSS和JS。 然后,在wordpress页面中调用modal。 无论我做什么,.modal- background始终在#mymodal前面。我发现这是一个z-索引问题,但是,由于#mymodal是在<body>结尾的内容div和.modal- background中生成的,z-index不能工作。 我所做的就是在生成后将#mymodal移到as的末尾:

$(document).ready(function () {
   $('#mymodal').on('shown.bs.modal', function () {
       $('#mymodal').appendTo(document.body);
     })
});

顺便说一下,我必须在$(document)中添加show .bs.modal。ready,因为当我按下按钮时,它并没有被调用。

@Muhd提供的解决方案是最好的方法。但如果你陷入了无法更改页面结构的情况,可以使用以下技巧:

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="false" style="background-color: rgba(0, 0, 0, 0.5);">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"
                aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title" id="myModalLabel">Modal title</h4>
        </div>
        <div class="modal-body">...</div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
        </div>
    </div>
</div>

这里的技巧是data- background ="false" style="background-color: rgba(0, 0, 0, 0.5);"通过删除默认背景并通过设置对话框本身的背景色来创建一个虚拟背景。

更新1: 正如@Gustyn指出的那样,点击背景并不能像预期的那样关闭对话框。为了实现这个,你必须添加一些java脚本代码。这里有一些例子,你可以实现这一点。

$('.modal').click(function(event){
    $(event.target).modal('hide');
});

删除模式背景背景设置为黑色在你的引导CSS

我看到:

.modal-backdrop {
    position: fixed; /*---- commented and it shows */
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1030;
    /*background-color: #000000; */
}

然后添加这个背景色和o.5的不透明度。我看到:

.modal-backdrop {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1030; 
  background-color: rgba(0, 0, 0, 0.5);
  opacity: 0.5;

}

注意:虽然这些变化是在boostrap css中做出的,但我使用了angular-strap,我没有下载或使用ng-animate css

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

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

在我的情况下,解决方案是简单的,我忘记在div上添加id“my-modal”的“modal”类,由$('#my-modal').modal('show')提升;

因为div的内容包含了所有需要的内容,所以除了上面的覆盖之外,屏幕上的结果几乎是完美的。

我敢打赌,其他与z-index技巧或其他css技巧无关的父div或未关闭div的位置的答案也可能是在这种情况下。