开发者

How to use execCommand to set a new line?

开发者 https://www.devze.com 2023-03-27 20:32 出处:网络
I want to insert a line break and set the caret position in the new line. I have this <!-- editable -->

I want to insert a line break and set the caret position in the new line.

I have this

<!-- editable -->
<div>
    hello
</div>

Then I add a new line

document.execCommand("insertHtml", false, "world" + "<br> ");

But the caret is not moved after the <br>, so when I type the text doesn't go to the new lin开发者_开发知识库e.

How can I set the caret position in the new line so when I type it goes bellow?

Actual result:

helloworld<typedtext>

Expected result:

helloworld
<typedtext>

Example on jsFiddle

Tested on Chrome 15


just add a line break and it will move it in chrome so some thing like this

document.execCommand("insertHtml", false, "world");
document.execCommand('insertParagraph',false); 

Question was asked a while ago but hope it helps someone anyway


document.execCommand('insertText', true, 'hi\\r\\nworld')
0

精彩评论

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

关注公众号