Maximum call stack size exceeded on trigger click [duplicate]

谁都会走 提交于 2019-12-29 08:29:09

问题


I really want to do something simple. On click on a certain element, i trigger a click on another element but i get the below error on my console.

Uncaught RangeError: Maximum call stack size exceeded

My code is as below;

$('body').on('click', '.actual-click-element', function(event) { 
    $('.trigger-click-element').trigger('click');
    event.preventDefault(); 
});

I wonder why am am getting this error and i don't see how this is recursive. Any ideas?


回答1:


Surely because .trigger-click-element is descendant of .actual-click-element...

To avoid recursive call, you could use jq triggerHandler():

Events triggered with .triggerHandler() do not bubble up the DOM hierarchy; if they are not handled by the target element directly, they do nothing.

$('body').on('click', '.actual-click-element', function(event) { 
    $('.trigger-click-element').triggerHandler('click');
    event.preventDefault(); 
});

Now if $('.trigger-click-element') returns more than one element, you could use:

$('.trigger-click-element').each(function(){$(this).triggerHandler('click');});


来源:https://stackoverflow.com/questions/39620646/maximum-call-stack-size-exceeded-on-trigger-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!