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

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


当前回答

但是迟了,这里有一个通用的解决方案

    var checkeventcount = 1,prevTarget;
    $('.modal').on('show.bs.modal', function (e) {
        if(typeof prevTarget == 'undefined' || (checkeventcount==1 && e.target!=prevTarget))
        {  
          prevTarget = e.target;
          checkeventcount++;
          e.preventDefault();
          $(e.target).appendTo('body').modal('show');
        }
        else if(e.target==prevTarget && checkeventcount==2)
        {
          checkeventcount--;
        }
     });

访问这个链接-当使用固定的侧边栏时,引导3模式在背景下消失。

其他回答

只需添加两行CSS:

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

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

和Davide Lorigliola一样,我通过提高z指数来解决这个问题:

.modal-backdrop { z-index: -999999; }
function addclassName(){
    setTimeout(function(){
        var c = document.querySelectorAll(".modal-backdrop");
        for (var i = 0; i < c.length; i++) {
            c[i].style.zIndex =  1040 + i * 20  ;
        }
        var d = document.querySelectorAll(".modal.fade");
        for(var i = 0; i<d.length; i++){
            d[i].style.zIndex = 1050 + i * 20;
        }
    }, 10);
}

更新:原来的JS方法导致了我的模态的多个克隆 如果组件被初始化多次,则附加到主体。相反,首先确认模式不是 已经添加:

function AttachModalsToBody(modalID) {
    var modal = $("body").children('#' + modalID);
    if ($("body").children('#' + modalID).length == 0) {
        $('#' + modalID).appendTo("body");
    }
}

在Blazor(在那里我使用Bootstrap 5.0),我做了以下(改编自@AdamAlbright的回答),以防止模式出现在背景后面:

在我的项目中,每个模态都在自己独立的组件中,因为我需要从其他组件中设置它们的参数,显然不可能将模态移动到这些父组件之外。因此,当它第一次呈现时,我使用JS Interop将每个模态附加到主体。

例如:MyModal.razor

在每个模态/组件内部:

@inject IJSRuntime jsRuntime

<!--HTML Section omitted for brevity-->

    @code{
        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            if (firstRender)
                await jsRuntime.InvokeVoidAsync("AttachModalToBody", "#myModalID");
        }
    }

在JS文件中:

function AttachModalToBody(modalID) {
    $(modalID).appendTo("body");
}

我发现通过添加内联样式标签来设置“display: none”可以防止这个问题。代码的位置没有任何影响。