当它发生时
当 modal 还具有“fade”类时,不会触发显示的事件.bs.modal。而show .bs.modal 总是有效的。见 https://github.com/twbs/bootstrap/issues/11793
HTML:
<div class="modal fade" id="first" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">Hello world from first modal</div>
</div>
</div>
<div class="modal" id="second" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">Hello world from second modal</div>
</div>
</div>
jQuery:
$('.modal').on('shown.bs.modal', function() {
//fired only in second modal
console.info('shown.bs.modal');
});
$('.modal').on('show.bs.modal', function() {
//fired always
console.info('show.bs.modal');
});
解决方案
对于引导 v3.3.6,将第 1010 行替换为:
that.$element // wait for modal to slide in
问题是什么
查看第 1006-1015 行:
var e = $.Event('shown.bs.modal', { relatedTarget: _relatedTarget })
transition ?
that.$dialog // wait for modal to slide in
.one('bsTransitionEnd', function () {
that.$element.trigger('focus').trigger(e)
})
.emulateTransitionEnd(Modal.TRANSITION_DURATION) :
that.$element.trigger('focus').trigger(e)
如果没有过渡(没有淡入淡出类),事件e会立即触发(在 that.$element 上)。对于过渡,我不确定究竟是为什么,但不知何故,来自函数emulateTransitionEnd的bsTransationEnd事件不是由that.$dialog.one()处理的。但是有了that.$element,一切似乎都奏效了。