在Bootbox中仅更改特定模态框的宽度

8

我正在使用 Bootstrap 3 中的 Bootbox 4 在 IE(IE 8 / IE 9) 上,一切都按照预期工作。

在 Bootstrap 3 中,您可以像以下 CSS 设置模态框宽度,但是,任何类似此类的更改都会影响到我的所有模态框:

.modal-dialog {
    width:70% !important;
}

CSS、jQuery 或 Bootbox 的设置中是否有一种方法可以仅针对一个特定的modal更改Bootbox警示 modal 的宽度?Bootbox页面只有很短的文档,我在其他任何地方都找不到相关信息。
更新:
创建特定模态窗口的 JS(带有示例数据):
bootbox.dialog({
    message: " \
        <table class='table table-hover'> \
            <thead> \
                <tr> \
                    <th>Item Name</th> \
                    <th>Location</th> \
                    <th>Path</th> \
                    <th>Last Update</th> \
                </tr> \
            </thead> \
            <tbody> \
                <tr> \
                    <td>Item 1</td> \
                    <td>Navbar - Level 2</td> \
                    <td>Products - blabla</td> \
                    <td>Added by xxx on 05 Aug 2014</td> \
                </tr> \
            </tbody> \
        </table>",
    title: "Search Results",
    buttons: {
        main: {
            label: "Close",
            className: "btn-primary"
        }
    },
    className: "modal70"
});

以下是我的当前CSS:

.modal-dialog.modal70 {
    width:70% !important;
}
3个回答

14

试一下这个:

.modal70 > .modal-dialog {
    width:70% !important;
}


更新:

试一下演示


谢谢。我尝试将class: "modal70"添加到创建模态框的JS中,但这没有任何区别。同时,以下内容也无效:className: "modal70"。有什么想法是出了什么问题吗? - user2571510
请提供您的HTML模态框代码或创建模态框的JS代码,以提供精确的解决方案。 - Moshtaf
谢谢 - 没有区别。我进行了更多的研究,看起来问题出在使用标准Bootstrap模态框类的父div上。Bootstrap在这里提供了不同的模态框宽度类,所以我尝试将以下内容添加到我的JS中,但仍然没有变化,即使这看起来是更“官方”的方法:.find('div.modal-dialog').addClass('modal-lg')。 - user2571510
抱歉,在尝试您的最新更新时我打错了一个字 - 这个很好用!非常感谢您的帮助!只是想知道为什么Bootstrap没有这样的方法,如果他们已经有了相应的类。 :) - user2571510

6

就像更进一步的信息...

正如文档所述,您可以在构造函数中使用“大小”属性来控制模态框的大小。

将相关的Bootstrap模态框大小类添加到对话框包装器中。
有效值为“大”和“小”(默认值:null)

bootbox.dialog({
    size: <small|large>
});

请看代码片段以查看用法示例(最好在全屏模式下查看)。

$(".size-by-funcion-0").click(function() {
    bootbox.dialog({
        size: "null",
        title: "Default Size Modal Example",
        message: "...",
    });
});

$(".size-by-funcion-1").click(function() {
    bootbox.dialog({
        size: "small",
        title: "Small Size Modal Example",
        message: "...",
    });
});

$(".size-by-funcion-2").click(function() {
    bootbox.dialog({
        size: "large",
        title: "Large Size Modal Example",
        message: "...",
    });
});

/*********************/
// or make it dynamic... with only one function
$(".dynamic-size").click(function() {
    bootbox.dialog({
        size: $(this).data('size'),
        title: "Dynamic Size Modal Example",
        message: "...",
    });
});
p {
    cursor: pointer;
    background-color: #ccc;
}
<script type="text/javascript" src="//code.jquery.com/jquery-2.1.0.js"></script>
<script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script type="text/javascript" src="https://github.com/makeusabrew/bootbox/releases/download/v4.3.0/bootbox.min.js"></script>

<p class="size-by-funcion-0">Modal with the default size</p>
<p class="size-by-funcion-1">Modal with "size:small"</p>
<p class="size-by-funcion-2">Modal with "size:large"</p>

<hr />

<p class="dynamic-size" data-size="null">Modal with "size:default"</p>
<p class="dynamic-size" data-size="small">Modal with "size:small"</p>
<p class="dynamic-size" data-size="large">Modal with "size:large"</p>

更多的信息可以在源网站查看:http://bootboxjs.com/documentation.html

注意:需要使用Bootstrap 3.1.0或更新版本。


0
如果 Moshtaf 的解决方案不起作用,可以再加一行代码来完成它:
.modal70 > .modal-dialog {
    width:70% !important;
    max-width:70% !important;
}

这是因为Bootbox还包括了一个默认的max-width值,它可能会覆盖width的定义。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接