开发者

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

开发者 https://www.devze.com 2023-03-31 19:01 出处:网络
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.

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 (e.g. bold) but doesn't do anything in this case.

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

Thanks

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号