實現簡易版富文字編輯器

2022-03-31 09:39:28 字數 1222 閱讀 6118

我們通常是利用div和iframe實現文字編輯功能的,其中,如果要在div中編輯文字,需設定其contenteditable=「true」即可,如果要在iframe中編輯文字,需設定designmode=「on」即可。雖然兩種方法均可實現文字編輯,但大都是利用iframe來實現富文字編輯的。

(1)首先,使用iframe可以解決瀏覽器相容性問題,可以方便的在不同瀏覽器中獲取選中的文字;

(2)使用iframe編輯文字可以實現所見即所得的效果,相當於顯示了瀏覽器解析原始碼後的內容;

(3)使用iframe可是實現直接在iframe下的document中進行文字操作(通過iframe.contentwindow.document獲取iframe下的document),不會影響到當前文件。

利用iframe開發富文字編輯器時主要用到documen.execcommand(command,show,value)方法,其中

command:表示要執行的命令名稱,

show:boolean型別,表示是否向使用者顯示命令特定的對話方塊或訊息框,

value:string型別,表示要使用該命令分配的值。

加粗命令:documen.execcommand(bold,false,null);

斜體命令:documen.execcommand(italic,false,null);

下劃線命令:documen.execcommand(underline,false,null);

刪除線命令:documen.execcommand(strikethrough,false,null);

左對齊命令:documen.execcommand(justifyleft,false,null);

居中對齊命令:documen.execcommand(justifycenter,false,null);

右對齊命令:documen.execcommand(justifyright,false,null);

有序排序對齊命令:documen.execcommand(insertorderedlist,false,null);

無序排序對齊命令:documen.execcommand(insertunorderedlist,false,null);

documen.execcommand(command,show,value)方法的執行命令非常繁多,遠不止本文提到的一星半點,具體的執行命令可以去google上搜尋,這裡推薦微軟msdn上的execcommand method,鏈結位址

簡易富文字編輯器

緊接著,這個工作多了一塊內容 把內容同步到某個產品的觸屏 中。由於當下還沒有資源開發一款類似 秀公尺 的產品,最終決定在之前的基礎上增加如下步驟 內容管理平台中基於 vue.js 開發,富文字編輯器使用的是 quill。而 quill 簡潔易用的優點,如今成了它的缺點 它只保留特定的標籤和樣式,且沒...

富文字編輯器

關於使用富文字編輯器的一些小坑 官網 1.專案 片 editor fail function xhr,editor,result custominsert function insertimg,result,editor this.editor.create this.editor.txt.html...

富文字編輯器

富文字編輯器,rich text editor,簡稱 rte,它提供類似於 microsoft word 的編輯功能。常用的富文字編輯器 kindeditor ueditor ckeditor 在頁面中新增js 用於初始化kindeditorallowfilemanager 是否允許瀏覽伺服器已上傳...