javascript - 防止 contenteditable 自动聚焦在 Chrome 上的 Enter 按键上

标签 javascript google-chrome contenteditable

我有一个 contenteditable div,我可以在上面控制某些键的行为。特别是,我有这个:

$('#my_contenteditable').on('keydown', function(e) {
  // Some code...
  if (e.key == 'Enter') {
    $(this).blur();
    e.preventDefault();
    return;
  }
  // Some more code...
});

但是在我模糊元素之后,当我再次按 Enter 时,Chrome 想要重新聚焦它。

JSFiddle demo here

我可以阻止这种行为吗?

最佳答案

if (e.keyCode == 13) {
    e.preventDefault()
    e.target.blur()
    window.getSelection().removeAllRanges()
}

关于javascript - 防止 contenteditable 自动聚焦在 Chrome 上的 Enter 按键上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44286640/

相关文章:

javascript - scrollTop() 和 slideDown() 不能正常工作

javascript - 跳过 CKEditor 中的元素

javascript - 获取span的内容

javascript - 使用 javascript 对项目列表进行排序

javascript - 通过脚本实现等高

javascript - 在 google chrome 扩展中解析并显示特定网站或博客的 xml feed!

javascript - 使用 chrome.storage.get 获取变量

Javascript OAuth 和 Opera 链接

javascript - 在 contenteditable 中模拟拖动类型文本/普通行为

JavaScript:如何获取用户IP地址?