触发我的.click回调中的点击事件会导致“超出最大调用堆栈大小” [英] Triggering click event inside my .click callback causes "Maximum call stack size exceeded"
问题描述
我无法得出任何可能的原因,为什么会发生这种情况。
我有一个打开引导弹出窗口的按钮。但在打开模式之前,我需要修改一些变量,所以我在隐藏按钮上触发了一个点击事件,这会打开引导模式。
HTML看起来像 -
< div id =visible-button>
< span class =hiddenid =open-modaldata-toggle =modaldata-target =#popup-modal>< / span>
< / div>
而js代码就像 -
<点击(函数(){
//执行数据操作
$('#open-modal')。pre>
$('#visible-button')。触发('点击');
});
但是这会产生
未捕获的RangeError:超过最大调用堆栈大小
如果我将代码编辑为
$('#visible-button')。click(function(){
//执行数据操作
setTimeout(function(){
$('#open-modal')。trigger('click');
},500);
});
模态不断打开并消失。
这里究竟发生了什么?我过去也触发了点击事件,但从未遇到过这种情况。
感谢您在Advance中的帮助。
可见按钮
中,触发打开模式
的点击等同于触发点击可见按钮
。所以当你点击可见按钮时,可见按钮
,并进入无限循环。 I couldn't get any possible reason as to why is this happening. I have a button which opens a bootstrap popup. But before opening the modal I need to alter some of my variables, So I am triggering a click event on hidden button which opens the bootstrap modal. HTML looks something like-
<div id="visible-button">
<span class="hidden" id="open-modal" data-toggle="modal" data-target="#popup-modal"></span>
</div>
And the js code is something like-
$('#visible-button').click(function(){
//perform data manipulation
$('#open-modal').trigger('click');
});
But this produces
Uncaught RangeError: Maximum call stack size exceeded
If I edit the code to
$('#visible-button').click(function(){
//perform data manipulation
setTimeout(function(){
$('#open-modal').trigger('click');
}, 500);
});
The modal keeps opening and dissappearing for ever. What is exactly happening here? I have triggered click event in the past too but have never faced such a scenario. Thanks for the help in Advance.
As open-modal
is wrapped inside visible-button
, triggering a click on open-modal
is equivalent to triggering a click on visible-button
. So when you click on visible button, onclick event of visible-button
gets triggered which intern clicks on visible-button
again, and it gets into a infinite loop.
这篇关于触发我的.click回调中的点击事件会导致“超出最大调用堆栈大小”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!