jQuery 太多递归

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2320157/
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-26 13:14:36  来源:igfitidea点击:

jQuery too much recursion

jqueryjquery-uirecursion

提问by Kevin Prince

Im trying to select a radio box when I click an LI. But i get the error "to much recursion".

当我单击 LI 时,我试图选择一个单选框。但是我得到了“太多递归”的错误。

Code is:

代码是:

$('li').click( function(){
     $('li.selected').removeClass('selected');
     $(this).addClass('selected');
     $(this).children("input[type=radio]").click();
});

This is using jQuery 1.4.2 and UI 1.7.2.

这是使用 jQuery 1.4.2 和 UI 1.7.2。

回答by MightyE

when you .click() the child input, the event bubbles up and re-triggers the li's click(). You need to add a .click() to the inputand do event.preventBubble=true;in it, or else just set the checked property instead of click()ing it.

当您 .click() child 时input,事件会冒泡并重新触发li的 click()。您需要添加一个 .click()inputevent.preventBubble=true;在其中执行,否则只需设置 checked 属性而不是click()ing 它。

回答by naugtur

Yes, it's event bubbling. Event bubbles up to li

是的,这是事件冒泡。事件冒泡到 li

You just have to do this:

你只需要这样做:

$('li').click( function(e){
  if($(e.target).is('li')){
     $('li.selected').removeClass('selected');
     $(this).addClass('selected');
     $(this).children("input[type=radio]").click();
  }
});

Don't add more events below, it's messy

下面不要再添加事件了,乱七八糟的

回答by Souvik Ghosh

Just in case this helps anyone, this message may show up for various reasons, as described by other contributors, it might be a case of Even bubble. But for me it was a bug in my code which resulted in a never ending function call. A function was calling itself, which was calling itself again, in other words - never ending recursion.

以防万一这对任何人有帮助,此消息可能会出于各种原因显示,如其他贡献者所述,它可能是偶数气泡的情况。但对我来说,这是我代码中的一个错误,导致了一个永无止境的函数调用。一个函数正在调用自己,它又在调用自己,换句话说 - 永无止境的递归。

So, just inspect your code and see if there is a situation where jQuery falls into a never ending loop of calling some block.

因此,只需检查您的代码,看看是否存在 jQuery 陷入调用某个块的永无止境循环的情况。

回答by Ukuser32

As a generic answer - the problem appears to be that jquery has too much work to do - i.e. looping through elements etc. I had this problem and when I inspected the page in firebug there were too many elements that I was expecting jquery to tackle.

作为一个通用的答案 - 问题似乎是 jquery 有太多工作要做 - 即循环元素等。我遇到了这个问题,当我在 firebug 中检查页面时,我希望 jquery 解决太多元素。

In firefox Javascript debugger you can click the > next to the error and it will give you more detail as to what is triggering it.

在 firefox Javascript 调试器中,您可以单击错误旁边的 >,它将为您提供有关触发它的更多详细信息。