Breaking out and/or removing a paragraph in a contenteditable block

I have recently been playing with contenteditable quite a lot. I've knocked up a simple editor prototype which I'm fairly happy with. There is one thing that's puzzling me.

If I have a paragraph and my cursor (caret) is inside it like so:

<p>some text |</p>

How do I break out of the paragraph? Is there a key combination or a execCommand that can be used to break out of it on the next line?

If I place the cursor (caret) at the end of the paragraph and press enter a new empty p tag is generated. This is fine normally when writing text blocks. But say I want to insert an image or codeblock. I don't want this to be inside a p tag but I can't see a way to break out.

I've tried highlighting the whole p block and then use:

document.execCommand('removeFormat', false, null);

Which works for other inline formatting (eg bold) but doesn't do anything in this case.

Here is a JsFiddle example, that might make more sense.

Thanks

链接地址: http://www.djcxy.com/p/74324.html

上一篇: 如何在contenteditable div中专门设置插入位置

下一篇: 在contenteditable块中分出和/或删除段落