Javascript Chrome/jQuery Uncaught RangeError: 超出最大调用堆栈大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7658775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:05:34  来源:igfitidea点击:

Chrome/jQuery Uncaught RangeError: Maximum call stack size exceeded

javascriptjquerygoogle-chromewebkitstack-overflow

提问by Andy

I am getting the error "Uncaught RangeError: Maximum call stack size exceeded" on chrome. here is my jQuery function

我在 chrome 上收到错误“Uncaught RangeError: Maximum call stack size exceeded”。这是我的 jQuery 函数

$('td').click(function () {
        if ($(this).context.id != null && $(this).context.id != '') {
            foo($('#docId').val(), $(this).attr('id'));
        }
        return false;
    });

Note that there are tens of thousands of cells in the page. However, I generally associate stack overflows with recursion and in this case as far as I can see there is none.

请注意,页面中有数以万计的单元格。但是,我通常将堆栈溢出与递归联系起来,在这种情况下,据我所知,没有。

Does creating a lambda like this automatically generate a load of stuff on the stack? is there any way round it?

创建这样的 lambda 会自动在堆栈上生成大量内容吗?有什么办法解决吗?

At the moment the only workaround I have is to generate the onclick events explicitly on each cell when rendering the HTML, which makes the HTML much larger.

目前,我唯一的解决方法是在呈现 HTML 时在每个单元格上显式生成 onclick 事件,这会使 HTML 变得更大。

回答by vantrung -cuncon

As "there are tens of thousands of cells in the page" binding the click-event to every single cell will cause a terrible performance problem. There's a better way to do this, that is binding a click event to the body & then finding out if the cell element was the target of the click. Like this:

由于“页面中有数以万计的单元格”,将点击事件绑定到每个单元格会导致严重的性能问题。有一个更好的方法来做到这一点,即将点击事件绑定到正文,然后找出单元格元素是否是点击的目标。像这样:

$('body').click(function(e){
       var Elem = e.target;
       if (Elem.nodeName=='td'){
           //.... your business goes here....
           // remember to replace $(this) with $(Elem)
       }
})

This method will not only do your task with native "td" tag but also with later appended "td". I think you'll be interested in this article about event binding & delegate

此方法不仅可以使用本机“td”标签完成您的任务,还可以使用稍后附加的“td”。我想你会对这篇关于事件绑定和委托的文章感兴趣



Or you can simply use the ".on()" method of jQuery with the same effect:

或者你可以简单地使用jQuery的“ .on()”方法,效果相同:

$('body').on('click', 'td', function(){
        ...
});

回答by John Durden

You can also get this error when you have an infinite loop. Make sure that you don't have any unending, recursive self references.

当您有无限循环时,您也可能收到此错误。确保您没有任何无休止的递归自引用。

回答by Jsonras

Mine was more of a mistake, what happened was loop click(i guess) basically by clicking on the login the parent was also clicked which ended up causing Maximum call stack size exceeded.

我的更像是一个错误,发生的事情是循环单击(我猜)基本上是通过单击登录名来单击父级也被单击,这最终导致超出了最大调用堆栈大小。

$('.clickhere').click(function(){
   $('.login').click();
});

<li class="clickhere">
  <a href="#" class="login">login</a>
</li>

回答by Silvio Delgado

This problem happened with me when I used jQUery Fancybox inside a website with many others jQuery plugins. When I used the LightBox (site here) instead of Fancybox, the problem is gone.

当我在带有许多其他 jQuery 插件的网站中使用 jQUEry Fancybox 时,我遇到了这个问题。当我使用 LightBox(此处站点)而不是 Fancybox 时,问题就消失了。

回答by demenvil

U can use

你可以使用

  $(document).on('click','p.class',function(e){
   e.preventDefault();
      //Code 
   });

回答by dev4life

I recently just ran into this issue as well. I had a very large table in the dialog div. It was >15,000 rows. When the .empty() was called on the dialog div, I was getting the error above.

我最近也遇到了这个问题。我在对话框 div 中有一个非常大的表。它是> 15,000 行。当在对话框 div 上调用 .empty() 时,我收到了上面的错误。

I found a round-about solution where before I call cleaning the dialog box, I would remove every other row from the very large table, then call the .empty(). It seemed to have worked though. It seems that my old version of JQuery can't handle such large elements.

我找到了一个迂回的解决方案,在我调用清理对话框之前,我会从非常大的表中删除每隔一行,然后调用 .empty()。不过它似乎奏效了。看来我的旧版 JQuery 无法处理这么大的元素。