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

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


当前回答

根据之前的答案,这可能有几个原因。 对我来说,问题是在不知情的情况下引用两个不同的Bootstrap链接,所以删除一个就解决了问题。

在我的例子中,我包括了这个:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css"
      integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">

还有一个离线的,我的笔记本电脑里已经有了。

其他回答

另外,确保BootStrap的css和js版本是相同的。不同版本也可以使模态出现在背景下:

例如:

Bad:

<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>

好:

<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>

对于那些像我这样不使用插件或主题就添加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,因为当我按下按钮时,它并没有被调用。

角:

($('.modal-backdrop') as any).remove();

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

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

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

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

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

添加到你的样式表:

.modal-backdrop {
    background-color: #000;
    bottom: 0;
    left: 0;
    position: fixed;
    right: 0;
    top: 0;
    z-index: 0 !important;
}