admin管理员组文章数量:1194548
I tried the following code on Chrome:
document.execCommand("insertBrOnReturn", false, true);
/
Wether I set the last parameter to true or false, the behaviour doesn't change: on return, new <div>
elements will be added.
Must have missed something... any ideas?
I tried the following code on Chrome:
document.execCommand("insertBrOnReturn", false, true);
http://jsfiddle.net/uVcd5/
Wether I set the last parameter to true or false, the behaviour doesn't change: on return, new <div>
elements will be added.
Must have missed something... any ideas?
Share Improve this question asked Dec 12, 2013 at 19:29 MatthewMatthew 11.3k11 gold badges55 silver badges70 bronze badges3 Answers
Reset to default 14 +50insertBrOnReturn
is a Mozilla specific command, Chrome doesn't support it. You can test that with:
document.queryCommandSupported('insertBrOnReturn')
jsFiddle here, it alert true
in Firefox, but false
in Chrome.
If you want to insert <br>
only, try:
document.execCommand('insertHTML', false, '<br><br>');
Also, check this: Prevent contenteditable adding <div> on ENTER - Chrome
document.execCommand("insertLineBreak");
According to: https://www.w3schools.com/jsref/met_document_execcommand.asp
You can check the specs command at: https://w3c.github.io/editing/docs/execCommand/#dfn-the-insertlinebreak-command
I came across this answer but didn't like how in Chrome if your cursor is at the beginning of a paragraph it adds two breaks. Changing the second <br>
to \u200C
makes Chrome work perfectly, not so much for Safari.
document.execCommand("insertHTML", false, "<br>\u200C");
What is \u200c?
Not sure. Found it referenced here.
Dealing with line Breaks on contentEditable DIV
本文标签: javascriptHow does execCommand quotinsertBrOnReturnquot workStack Overflow
版权声明:本文标题:javascript - How does execCommand "insertBrOnReturn" work? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738504888a2090479.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论