簡體   English   中英

execCommand“insertBrOnReturn”如何工作?

[英]How does execCommand “insertBrOnReturn” work?

我在 Chrome 上嘗試了以下代碼:

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

http://jsfiddle.net/uVcd5/

無論我將最后一個參數設置為 true 還是 false,行為都不會改變:返回時,將添加新的<div>元素。

一定錯過了什么......有什么想法嗎?

insertBrOnReturnMozilla特定的命令, Chrome不支持它。 您可以使用以下方法進行測試:

 document.queryCommandSupported('insertBrOnReturn')

jsFiddle 在這里,它在Firefox 中警告為true ,但在Chrome 中為false

如果您只想插入<br> ,請嘗試:

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

另外,請檢查: 防止 contenteditable 在 ENTER 上添加 <div> - Chrome

document.execCommand("insertLineBreak");

根據: https : //www.w3schools.com/jsref/met_document_execcommand.asp

您可以在以下位置檢查規范命令: https : //w3c.github.io/editing/docs/execCommand/#dfn-the-insertlinebreak-command

我遇到了這個答案,但不喜歡在 Chrome 中,如果您的光標位於段落的開頭,它會添加兩個中斷。 將第二個<br>更改為\‌可以使 Chrome 完美運行,而對於 Safari 則不然。

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

什么是\‌? 不確定。 找到它引用here。

處理 contentEditable DIV 上的換行符

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM