我已经看到很多关于引导事件的答案,例如 hide.bs.modal 当模态关闭时触发。
hide.bs.modal
这些事件存在问题:模态中的任何弹出窗口(弹出窗口,工具提示等)都会触发该事件。
当模态关闭时,还有另一种方法可以捕获事件。
$(document).on('hidden','#modal:not(.in)', function(){} );
Bootstrap使用 in 模态打开时的类。 使用它是非常重要的 hidden 自上课以来的活动 in 仍然在事件发生时定义 hide 被触发了。
in
hidden
hide
此解决方案在IE8中不起作用,因为IE8不支持Jquery :not() 选择。
:not()
我和其他人有同样的问题
$('#myModal').on('hidden.bs.modal', function () { // do something鈥� })
您需要将其放在页面底部,将其放在顶部永远不会触发事件。
我在这里超级跳得很晚,但对于使用Bootstrap模态和React的人我一直在使用 MutationObserver 检测模态可见性的变化并相应地调整状态 - 此方法可用于在模态关闭时运行任何函数:
//react stuff componentDidMount: function() { var self = this; $(function() { $("#example_modal").addClass('modal'); //use MutationObserver to detect visibility change //reset state if closed if (MutationObserver) { var observer = new MutationObserver(function(mutations) { //use jQuery to detect if element is visible if (!$('#example_modal').is(':visible')) { //reset your state self.setState({ thingone: '', thingtwo: '' }); } }); var target = document.querySelector('#example_modal'); observer.observe(target, { attributes: true }); } }); }
对于那些对现代浏览器支持感到疑惑的人,CanIUse有MutationObserver的报道 约87%
希望有人帮助:)
干杯,
可靠的人
我会这样做:
$('body').on('hidden.bs.modal', '#myModal', function(){ //this call your method });
其余的已经是其他人写的。我还建议阅读文档: jquery - 关于方法
$('#myModal').on('hidden.bs.modal', function () { // do something鈥n})
Bootstrap 3: getbootstrap.com/javascript/#modals-events
Bootstrap 4: getbootstrap.com/docs/4.1/components/modal/#events
$('#myModal').on('hidden', function () { // do something鈥n})
看到 getbootstrap.com/2.3.2/javascript.html#modals 鈫鈫活动
Bootstrap 4:
$('#myModal').on('hidden.bs.modal', function (e) { // call your method })
的 hide.bs.modal 强> : 调用hide实例方法时会立即触发此事件。
的 hidden.bs.modal 强> : 当模态完成对用户的隐藏时将触发此事件(将等待CSS转换完成)。
开始 的 Bootstrap 3 强> ( 的 编辑: 强> 还是一样的 的 Bootstrap 4 强> )有2个实例可以启动事件,是:
$('#myModal').on('hide.bs.modal', function () { console.log('Fired at start of hide event!'); });
$('#myModal').on('hidden.bs.modal', function () { console.log('Fired when hide event has finished!'); });
参考: http://getbootstrap.com/javascript/#js-events
您需要使用“on”事件,而不是“live”,而是将其分配给文档对象:
使用:
$(document).on('hidden.bs.modal', '#Control_id', function (event) { // code to run on closing });
$(document.body).on('hidden.bs.modal', function () { $('#myModal').removeData('bs.modal') });