输入的Contenteditable段落标记 [英] Contenteditable paragraph tag on enter

查看:120
本文介绍了输入的Contenteditable段落标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道是否有一种可接受的方式来强制所有主流浏览器插入段落标记,而不是他们插入的默认标记,并在contentEditable为true时按回车键。据我所知IE自动插入p。但谷歌Chrome插入div标签和Firefox插入br(WTF ?!)。

提前致谢! 您可以使用 document.execCommand('formatBlock',false,'p'); 在事件如 keypress keydown 等等在输入新闻后使用段落。例如:
$ b $ pre $ lt; code> element.addEventListener('keypress',function(ev){
if(ev.keyCode = ='13')
document.execCommand('formatBlock',false,'p');
},false);


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!

解决方案

you can use document.execCommand('formatBlock', false, 'p'); in event like keypress or keydown, etc. to use paragraphs after enter press. For example:

element.addEventListener('keypress', function(ev){
    if(ev.keyCode == '13')
        document.execCommand('formatBlock', false, 'p');
}, false);

这篇关于输入的Contenteditable段落标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆