2016-11-17 126 views
2

爲什麼我收到此錯誤:的jQuery()方法調用的最大堆棧大小超過

Uncaught RangeError: Maximum call stack size exceeded

這裏是我的代碼:

$(document).on('keypress focusout', '.checklist-item-input', function (e) { 
    if (e.which == 13 || e.type == 'focusout') { 
    $('.checklist-item').removeClass('edit'); 
    $(this).siblings('.checklist-item-detail').text($(this).val()); 
    $(this).blur(); 
    $('.checklist-item-detail').each(function() { 
     if (!$(this).text().length) { 
     $(this).closest('.checklist-item').parent().remove(); 
     } 
    }); 
    } 
}); 
+4

「原因focusout'你叫'.blur()'這引起了'focusout'它調用'.blur()'這引起了調用'.blur()'調用'.blur()'調用'.blur()'調用'.blur()'調用'.blur()'調用'.blur()'調用'.blur()'' ....等 –

+2

'$(this).blur();'執行聚焦事件,無限次稱爲 –

回答

0

正如其他人提到你有效地使一個遞歸調用。一個簡單的解決方法是增加一個定點變量來阻止它會遞歸:

var busy = false; 
$(document).on('keypress focusout', '.checklist-item-input', function (e) { 
    if (!busy && e.which == 13 || e.type == 'focusout') { 
    busy = true; 
    $('.checklist-item').removeClass('edit'); 
    $(this).siblings('.checklist-item-detail').text($(this).val()); 
    $(this).blur(); 
    $('.checklist-item-detail').each(function() { 
     if (!$(this).text().length) { 
     $(this).closest('.checklist-item').parent().remove(); 
     } 
    }); 
    busy = false; 
    } 
}); 
上`
+0

或檢查'$(「:focus」)' - http://stackoverflow.com/questions/497094/how-do-i-find-out-which-dom-element-have-the-focus –

+0

@ freedomn-m:是的,當然也可以檢查當前焦點。我是一個用於任何類型事件的通用哨兵包裝。 –

+0

我試過你的代碼,但它給我同樣的錯誤! @GoneCoding –

相關問題