【发布时间】:2019-06-10 17:09:21
【问题描述】:
我有这个例子中的 Bootstrap Modals,我需要拦截它们的 Open/Show 事件。但是this thread 中提到的任何建议都不适合我。我错过了什么吗?
我尝试了以下方法:
// Listener - Show Event Interception
// NONE OF THE FOLLOWING WORK
$('#modal').on('shown.bs.modal', function (e) {
alert('Modal opened 1');
});
$('#modal').on('shown', function (e) {
alert('Modal opened 2');
});
$(window).on('shown.bs.modal', function() {
alert('Modal opened 3');
});
我的 Modal 看起来像这样(用$('#modal').show(); 打开):
<div class="modal modal-dialog" id="modal" tabindex="-1" role="dialog" aria-hidden="true" >
<div class="modal-content" style="margin:0 auto;">
...
</div>
</div>
更新
当前的问题是我使用的是show() 而不是modal('show')。但是我也尝试使用$(document).on('show', function(evt) {..} 进行拦截,但失败了。
拦截 jQuery show() 事件的新方法:
$(document).on('show', function(evt) {
if($(evt.target).attr('role') != null && $(evt.target).attr('role') == 'dialog') {
alert('Intercepted Dialog Show');
}
});
【问题讨论】: