简体   繁体   English

execCommand“insertBrOnReturn”如何工作?

[英]How does execCommand “insertBrOnReturn” work?

I tried the following code on Chrome:我在 Chrome 上尝试了以下代码:

document.execCommand("insertBrOnReturn", false, true);

http://jsfiddle.net/uVcd5/ 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.无论我将最后一个参数设置为 true 还是 false,行为都不会改变:返回时,将添加新的<div>元素。

Must have missed something... any ideas?一定错过了什么......有什么想法吗?

insertBrOnReturn is a Mozilla specific command, Chrome doesn't support it. insertBrOnReturnMozilla特定的命令, Chrome不支持它。 You can test that with:您可以使用以下方法进行测试:

 document.queryCommandSupported('insertBrOnReturn')

jsFiddle here , it alert true in Firefox , but false in Chrome . jsFiddle 在这里,它在Firefox 中警告为true ,但在Chrome 中为false

If you want to insert <br> only, try:如果您只想插入<br> ,请尝试:

document.execCommand('insertHTML', false, '<br><br>');

Also, check this: Prevent contenteditable adding <div> on ENTER - Chrome另外,请检查: 防止 contenteditable 在 ENTER 上添加 <div> - Chrome

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.我遇到了这个答案,但不喜欢在 Chrome 中,如果您的光标位于段落的开头,它会添加两个中断。 Changing the second <br> to \‌ makes Chrome work perfectly, not so much for Safari.将第二个<br>更改为\‌可以使 Chrome 完美运行,而对于 Safari 则不然。

document.execCommand("insertHTML", false, "<br>\u200C");

What is \‌?什么是\‌? Not sure.不确定。 Found it referenced here.找到它引用here。

Dealing with line Breaks on contentEditable DIV 处理 contentEditable DIV 上的换行符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM