开发者

How to use execCommand to set a new line?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜