强制IE contentEditable元素在Enter键上创建换行符,而不会中断撤销 [英] Force IE contentEditable element to create line breaks on Enter key, without breaking Undo
问题描述
在Internet Explorer上,每次按Enter键时,contentEditable DIV都会创建一个新段落(< p>< / p>
),而Firefox会创建< br />
标记。
On Internet Explorer, a contentEditable DIV creates a new paragraph (<p></p>
) each time you press Enter whereas Firefox creates a <br/>
tag.
正如讨论这里,可以使用JavaScript拦截Enter键按下并使用range.pasteHTML改为创建< br />
。但是这样做会打破Undo菜单;
As discussed here, it is possible to use JavaScript to intercept the Enter keyPress and use range.pasteHTML to create a <br/>
instead. But doing this breaks the Undo menu; once you hit Enter, you can no longer Undo past that point.
如何强制contentEditable元素在Enter上创建单行换行而不破坏撤销?
How can I force the contentEditable element to create single line breaks on Enter without breaking Undo?
推荐答案
不是一个确切的解决方案,而是另一种解决方法。如果您使用标记:
Not an exact solution, but another workaround. If you use the markup:
<div contenteditable="true">
<div>
content
</div>
<div>
然后按输入会创建新的 div
标记而不是 p
标记。
Then pressing enter will create new div
tags instead of p
tags.
这篇关于强制IE contentEditable元素在Enter键上创建换行符,而不会中断撤销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!