我正在制作一个Bootstrap网站,其中包含几个Bootstrap'Modals'。我正在尝试自定义一些默认功能。
问题是这样;您可以通过单击背景关闭模态。反正有禁用此功能?仅在特定模态上?
我正在制作一个Bootstrap网站,其中包含几个Bootstrap'Modals'。我正在尝试自定义一些默认功能。
问题是这样;您可以通过单击背景关闭模态。反正有禁用此功能?仅在特定模态上?
Answers:
在“选项”一章的链接页面中,您可以看到该backdrop
选项。将此值传递给值'static'
将防止关闭模态。
正如@PedroVagner在评论中指出的那样,您还可以通过按{keyboard: false}
来阻止关闭模式Esc。
如果您通过js打开模式,请使用:
$('#myModal').modal({backdrop: 'static', keyboard: false})
如果使用数据属性,请使用:
<button data-target="#myModal" data-toggle="modal" data-backdrop="static" data-keyboard="false">
Launch demo modal
</button>`
$('#modal').modal('show');
希望对您有所帮助之前!
这是最简单的一个
您可以定义模态行为,定义数据键盘和数据背景。
<div id="modal" class="modal hide fade in" data-keyboard="false" data-backdrop="static">
data-backdrop="static"
选项放在模式定义上,而不是按其他答案中指定的方式打开模式的触发按钮,对我有用。
您可以使用如下属性:data-backdrop="static"
或与javascript一起使用:
$('#myModal').modal({
backdrop: 'static',
keyboard: false // to prevent closing with Esc button (if you want this too)
})
也请参见以下答案:禁止关闭Twitter Bootstrap模式窗口
在我的应用程序中,我使用下面的代码通过jQuery显示Bootstrap模态。
$('#myModall').modal({
backdrop: 'static',
keyboard: true,
show: true
});
您可以使用
$.fn.modal.prototype.constructor.Constructor.DEFAULTS.backdrop = 'static';
$.fn.modal.prototype.constructor.Constructor.DEFAULTS.keyboard = false;
更改默认行为。
有两种方法可以在引导程序模型区域之外禁用点击以关闭模式-
使用JavaScript
$('#myModal').modal({
backdrop: 'static',
keyboard: false
});
在HTML标签中使用data属性
data-backdrop="static" data-keyboard="false" //write this attributes in that button where you click to open the modal popup.
结帐此::
$(document).ready(function() {
$("#popup").modal({
show: false,
backdrop: 'static'
});
$("#click-me").click(function() {
$("#popup").modal("show");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.1/css/bootstrap.css" rel="stylesheet"/>
<script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.1.0/css/bootstrap-combined.min.css" rel="stylesheet">
</head>
<body>
<div class="modal" id="popup" style="display: none;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3>Standard Selectpickers</h3>
</div>
<div class="modal-body">
<select class="selectpicker" data-container="body">
<option>Mustard</option>
<option>Ketchup</option>
<option>Relish</option>
</select>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Save changes</button>
</div>
</div>
<a id="click-me" class="btn btn-primary">Click Me</a>
</body>
<script type="text/javascript" src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.1.0/js/bootstrap.min.js"></script>
</html>
如果您不知道模态是否已经打开,并且需要配置模态选项,则可以使用另一个选项:
Bootstrap 3.4
var $modal = $('#modal');
var keyboard = false; // Prevent to close by ESC
var backdrop = 'static'; // Prevent to close on click outside the modal
if(typeof $modal.data('bs.modal') === 'undefined') { // Modal did not open yet
$modal.modal({
keyboard: keyboard,
backdrop: backdrop
});
} else { // Modal has already been opened
$modal.data('bs.modal').options.keyboard = keyboard;
$modal.data('bs.modal').options.backdrop = backdrop;
if(keyboard === false) {
$modal.off('keydown.dismiss.bs.modal'); // Disable ESC
} else { //
$modal.data('bs.modal').escape(); // Resets ESC
}
}
引导程序4.3+
var $modal = $('#modal');
var keyboard = false; // Prevent to close by ESC
var backdrop = 'static'; // Prevent to close on click outside the modal
if(typeof $modal.data('bs.modal') === 'undefined') { // Modal did not open yet
$modal.modal({
keyboard: keyboard,
backdrop: backdrop
});
} else { // Modal has already been opened
$modal.data('bs.modal')._config.keyboard = keyboard;
$modal.data('bs.modal')._config.backdrop = backdrop;
if(keyboard === false) {
$modal.off('keydown.dismiss.bs.modal'); // Disable ESC
} else { //
$modal.data('bs.modal').escape(); // Resets ESC
}
}
将选项更改为_config
_setEscapeEvent()
代替它.escape()
吗?
适用于我的解决方案如下:
$('#myModal').modal({backdrop: 'static', keyboard: false})
背景:禁用外部点击事件
键盘:禁用scape关键字事件
尝试这个:
<div
class="modal fade"
id="customer_bill_gen"
data-keyboard="false"
data-backdrop="static"
>
backdrop: 'static'
https://getbootstrap.com/docs/3.3/javascript/#modals-options
指定
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.
})
如果使用@ ng-bootstrap,请使用以下命令:
零件
import { Component, OnInit } from '@angular/core';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.scss'],
})
export class ExampleComponent implements OnInit {
constructor(
private ngbModal: NgbModal
) {}
ngOnInit(): void {
}
openModal(exampleModal: any, $event: any) {
this.ngbModal.open(exampleModal, {
size: 'lg', // set modal size
backdrop: 'static', // disable modal from closing on click outside
keyboard: false, // disable modal closing by keyboard esc
});
}
}
模板
<div (click)="openModal(exampleModal, $event)"> </div>
<ng-template #exampleModal let-modal>
<div class="modal-header">
<h5 class="modal-title">Test modal</h5>
</div>
<div class="modal-body p-3">
<form action="">
<div class="form-row">
<div class="form-group col-md-6">
<label for="">Test field 1</label>
<input type="text" class="form-control">
</div>
<div class="form-group col-md-6">
<label for="">Test field 2</label>
<input type="text" class="form-control">
</div>
<div class="text-right pt-4">
<button type="button" class="btn btn-light" (click)="modal.dismiss('Close')">Close</button>
<button class="btn btn-primary ml-1">Save</button>
</div>
</form>
</div>
</ng-template>
此代码已使用以下代码在角度9上进行了测试:
“ @ ng-bootstrap / ng-bootstrap”:“ ^ 6.1.0”,
“ bootstrap”:“ ^ 4.4.1”,
您也可以不使用JQuery来执行此操作,如下所示:
<div id="myModal">
var modal = document.getElementById('myModal');
modal.backdrop = "static";
modal.keyboard = false;