如何在用户点击关闭图像时关闭Bootstrap模式?

MID*_*MID 38 modal-dialog twitter-bootstrap

我想在用户点击关闭btn时关闭模态.我认为我需要从modal.js覆盖这部分代码:

 hide: function (e) {
    e && e.preventDefault()

    var that = this

    e = $.Event('hide')//if I delete this line modal won't hide

    this.$element.trigger(e)

    if (!this.isShown || e.isDefaultPrevented()) return

    this.isShown = false

    $('body').removeClass('modal-open')

    escape.call(this)

    this.$element.removeClass('in')

    $.support.transition && this.$element.hasClass('fade') ?
      hideWithTransition.call(this) :
      hideModal.call(this)
Run Code Online (Sandbox Code Playgroud)

我在正确的道路上吗?

Per*_*mal 67

启动模态时,您可以传递选项:

{
  keyboard: false,
  backdrop: 'static'
}
Run Code Online (Sandbox Code Playgroud)

这将通过单击背景和转义按钮禁用关闭模式.或者可以将它们设置为data-属性.

  • 使用 `data-` 属性的示例:`<div class="modal fade" data-keyboard="false" data-backdrop="static">` (2认同)

Tab*_*res 65

您可以定义模态行为,定义数据键盘和数据背景.

 <div id="modal" class="modal hide fade in" data-keyboard="false" data-backdrop="static">
Run Code Online (Sandbox Code Playgroud)


Vik*_*ram 10

试试这个吧

<div id="myModal" class="modal hide fade in" data-backdrop="static">
<div> </div>
</div>
Run Code Online (Sandbox Code Playgroud)


Ang*_*odi 6

在Jquery中制作它的最佳方法是:

<script type="text/javascript">
    $('#modal-id').modal({
        backdrop: 'static',
        keyboard: false
    });
</script>
Run Code Online (Sandbox Code Playgroud)

或者在HTML中:

<div id="modal-id" class="modal hide fade in" data-keyboard="false" data-backdrop="static">
Run Code Online (Sandbox Code Playgroud)

但是,如果您已经初始化了模态,则需要取消绑定模态的click事件,例如:

<script type="text/javascript">
    //this remove the close button on top if you need
    $('#modal-id').find('.close').remove();
    //this unbind the event click on the shadow zone
    $('#modal-id').unbind('click');
</script>
Run Code Online (Sandbox Code Playgroud)