
contenteditable 编辑框中 shift+enter 换行后结构混乱
在使用 contenteditable 属性开启编辑功能的文本域中,当用户按住 Shift+Enter 时会出现换行添加
标签的问题,导致结构混乱。要解决此问题,可以使用以下方法:
在 keyDown 事件中,使用 event.preventDefault() 阻止默认动作,然后用 document.execCommand('insertParagraph') 插入一个段落。
function keyDown (event) {
if (event.shiftKey && event.keyCode === 13) {
event.preventDefault();
document.execCommand('insertParagraph');
return false;
}
document.execCommand('formatblock', false, '<p>');
}将上面的代码添加到示例中,在 Chrome 中测试,问题得到解决。
以上就是ContentEditable 编辑框中,如何解决 Shift+Enter 换行导致结构混乱的问题?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号