输入时可编辑内容段落标签
我想知道是否有一种可接受的方法来强制所有主要浏览器插入段落标记,而不是当 contentEditable 为 true 时按 Enter 键时插入的默认标记。
据我所知IE会自动插入p。但 Google Chrome 会插入 div 标签,而 Firefox 则会插入 br(WTF?!)。
提前致谢!
I was wondering if there is an acceptable way to force all major browsers to insert paragraph tag instead of the default tag that they insert on pressing enter key when contentEditable is true.
As far as I know IE inserts p automatically. But Google Chrome inserts div tag and Firefox inserts br (WTF?!).
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在
keypress
或keydown
等事件中使用document.execCommand('formatBlock', false, 'p');
来使用按 Enter 键后的段落。例如:you can use
document.execCommand('formatBlock', false, 'p');
in event likekeypress
orkeydown
, etc. to use paragraphs after enter press. For example:由于它是在浏览器中构建的,因此您无法更改该行为。您可以通过检测浏览器并相应地替换元素来解决此问题。非常丑陋,我知道。
另请检查 WhatWG 的背景: http://lists .whatwg.org/htdig.cgi/whatwg-whatwg.org/2011-May/031577.html
As its build in the browser you can't change that behaviour. You could work around by detecting browser and replacing elements correspondingly. Very ugly, I know.
Also check WhatWG for background: http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2011-May/031577.html
我遇到了同样的问题并找到了解决方案(CHROME、MSIE、FIREFOX),请按照链接中的代码进行操作。
https://jsfiddle.net/kzkxo70L/1/
I had this same problem and found the solution (CHROME, MSIE, FIREFOX), follow my code in the link.
https://jsfiddle.net/kzkxo70L/1/