我正在做一个bootstrap网站,与一对bootstrap 'Modals'。 我正在尝试自定义一些默认功能。

问题在于; 你可以通过点击背景来关闭模式。 有没有办法禁用这个功能? 只在特定的情态动词?

引导模态页面


当前回答

把这段代码放在modal html的第一个块中

引导4.倍

-keyboard = "和"日期-backdrop =“静态”

Boostrap 5.倍

data-bs-keyboard = " false " data-bs-backdrop =“静态”

例子:

<div id="modal-user" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true" data-bs-keyboard="false" data-bs-backdrop="static">

文档引导5:https://getbootstrap.com/docs/5.1/components/modal/#options

包括一个情态背景元素。或者,为背景指定静态,它不会在单击时关闭模式。

其他回答

你也可以不使用JQuery做到这一点,如下所示:

<div id="myModal">

var modal = document.getElementById('myModal');
modal.backdrop = "static";
modal.keyboard = false;

你可以使用

$.fn.modal.prototype.constructor.Constructor.DEFAULTS.backdrop = 'static';
$.fn.modal.prototype.constructor.Constructor.DEFAULTS.keyboard =  false;

更改默认行为。

这是最简单的

你可以定义你的模式行为,定义数据键盘和数据背景。

<div id="modal" class="modal hide fade in" data-keyboard="false" data-backdrop="static">
You can Disallow closing of #signUp (This should be the id of the modal) modal when clicking outside of modal.
As well as on ESC button.
jQuery('#signUp').on('shown.bs.modal', function() {
    jQuery(this).data('bs.modal').options.backdrop = 'static';// For outside click of modal.
    jQuery(this).data('bs.modal').options.keyboard = false;// For ESC button.
})

这些方法对我来说都没用。

我有一个条款和条件模式,我想强迫人们在继续之前审查…默认的“静态”和“键盘”选项使它不可能向下滚动页面,因为条款和条件是几页日志,静态不是我的答案。

因此,我去解除模式上的点击方法的绑定,与以下我能够得到想要的效果。

$('.modal').off('click');