Javascript load()被多次触发
我已经对模式使用了Javascript load()被多次触发,javascript,jquery,Javascript,Jquery,我已经对模式使用了off(),但是当模式第二次触发时,警报会多次触发,你知道为什么会发生这种情况以及我如何阻止它吗 $('#my_modal').modal('show').off('shown.bs.modal').on('shown.bs.modal', function() { $('#head').load('example.com/email.html', function(){ $.getScript('jquery.min.js', function(dat
off()
,但是当模式第二次触发时,警报
会多次触发,你知道为什么会发生这种情况以及我如何阻止它吗
$('#my_modal').modal('show').off('shown.bs.modal').on('shown.bs.modal', function() {
$('#head').load('example.com/email.html', function(){
$.getScript('jquery.min.js', function(data){
alert('debug');
})
});
});
一旦绑定了('show.bs.modal')上的
.on
,它将在每次加载modal时被激发
如果要在第一次显示后解除绑定,请尝试以下操作:
$('#my_modal').modal('show').on('shown.bs.modal', function() {
$('#my_modal').off('shown.bs.modal'); // might be able to use $(this).off('shown.bs.modal');
$('#head').load('example.com/email.html', function(){
$.getScript('jquery.min.js', function(data){
alert('debug');
})
});
});
事件是如何触发的?
.off('show.bs.modal')。on('show.bs.modal',…);
建议您最好委派事件。无论如何,您提供的上下文代码没有真正的意义,为什么您要在页面上重新包含jQuery?@A.Wolff这只是一个示例在本例中,使用one
而不是on
:.one('show.bs.modal',handler);
取而代之