我正在使用 localStorage 和 Contenteditable 制作一个基本任务列表,当我按下回车键(在 chrome 中)时,浏览器会添加一个新行。我想阻止这种默认行为并提交任务。 jQuery blur 移除焦点,但仅在添加新行之后。 我尝试使用,
return false
e.preventDefault();
e.stopPropagagtion();
但是它们都不起作用,有什么办法可以防止这种情况发生吗?
$('.taskContent').keyup(function(e) {
if(e.keyCode == 13) {
$('.task').removeClass('editing');
$('.task').children('a').fadeTo('medium', 0.5, function() {
localStorage.setItem('tasksData', tasks.innerHTML);
});
$('.taskContent').blur();
}
});
最佳答案
我以前遇到过这个问题,我认为问题在于事件是在 keydown 而不是 keyup 上触发的 return/enter/tab 和其他“特殊功能”键。
尝试将其更改为
$('.taskContent').keydown(function(e){});
关于jquery - 防止 HTML5 contenteditable 字段中 'return' 键的默认行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6018799/