CKEditor/FCKEditor 使用FCKeditor 2.6.5 快速使用教程(含插入圖片)
更新時間:2010年03月20日 20:09:37 作者:
CKEditor 是著名的 HTML 編輯器,IBM、Oracle、Adobe 等都在用。CKEditor 創(chuàng)建于 2003 年,其前身為 FCKEditor,在 2009 年的時候把“F”去掉了,更名為 CKEditor。
其開源協(xié)議是基于 GPL, LGPL 和 MPL 的。官方網(wǎng)站:http://ckeditor.com/
一般來說,我們在編輯內(nèi)容時,先是讀入到 textarea,再將 textarea 的內(nèi)容賦給編輯器。因?yàn)橹苯影褍?nèi)容作為字符串給編輯器的 Value 屬性賦值使用的是 JavaScript 代碼,要讓 JS 代碼不受內(nèi)容中雙引號、換行等的干擾,只有先讀入到 textarea 最方便。
使用 FCKeditor 2.6.5
<div><textarea id="fckcontent" name="content">cftea</textarea></div>
<script type="text/javascript" src="fckeditor/fckeditor.js"></script>
<script type="text/javascript">
<!--
var oFCKeditor = new FCKeditor("fckcontent");
oFCKeditor.BasePath = "fckeditor/"; // fckeditor 文件夾位置。
oFCKeditor.Create();
//-->
</script>
本來應(yīng)該用 display:none 將 textarea 隱藏起來,不過為了查看效果,這里不隱藏。
這樣編輯器就自動與 fckcontent 關(guān)聯(lián)起來了,打開網(wǎng)頁時 FCKeditor 自動讀取 textarea 的內(nèi)容,提交時又自動將其內(nèi)容(自動為 XHTML)賦給 textarea。
注意,我們 textarea 的 id 和 name 值不一樣,為什么呢?
這里應(yīng)該是這個版本不太完善的地方,如果我們把 textarea 的 id 和 name 值設(shè)置為一樣,那么 FCKeditor 文本區(qū)的 name 值也是 content,在服務(wù)器端 Request.Form("content").Count 就會有兩個,我們服務(wù)器端取值就稍稍有點(diǎn)不方便,得用 Request.Form("content")(0)。如果我們將 id 設(shè)為 fckcontent,那么 FCKeditor 文本區(qū)的 name 就是 fckcontent,與 textarea 不同名。
設(shè)置編輯器寬高
var oFCKeditor = new FCKeditor("fckcontent", 500, 300);
或
var oFCKeditor = new FCKeditor("fckcontent");
oFCKeditor.Width = 500;
oFCKeditor.Height = 300;
設(shè)置工具條
var oFCKeditor = new FCKeditor("fckcontent", 500, 300, "Basic");
注意第四個參數(shù),其可選值有 Basic、Default,注意大小寫不可搞錯,分別表示基本工具條、默認(rèn)工具條(全部按鈕)。
設(shè)置初始值、設(shè)置值、取值
設(shè)置初始值
實(shí)際上設(shè)置初始值很少用,因?yàn)橐话愣际桥c textarea 關(guān)聯(lián)的,故只是簡單列出來一下,不深究。說明一下,如果關(guān)聯(lián)的 textarea 存在,則賦初始值是沒有用的。
var oFCKeditor = new FCKeditor("fckcontent2", 500, 300, "Default", "腳本之家");
或
var oFCKeditor = new FCKeditor("fckcontent2", 500, 300, "Default");
oFCKeditor.BasePath = "fckeditor/";
oFCKeditor.Value = "cftea"; // 必須在 Create 之前
oFCKeditor.Create();
設(shè)置值
若要演示此示例,最好是放在按鈕的事件處理程序中,目的是有些延遲,否則會說 FCKeditorAPI 未定義。
var oEditor = FCKeditorAPI.GetInstance("fckcontent");
oEditor.SetHTML("腳本之家");
取值
若要演示此示例,最好是放在按鈕的事件處理程序中,目的是有些延遲,否則會說 FCKeditorAPI 未定義。
var oEditor = FCKeditorAPI.GetInstance("fckcontent");
alert(oEditor.GetXHTML()); // 還有個類似方法是 GetHTML,但不推薦用 GetHTML。
您這樣做很危險:
var oEditor = FCKeditorAPI.GetInstance("fckcontent");
oEditor.SetHTML("腳本之家");
alert(oEditor.GetXHTML()); // 這里的值并不一定是上一句賦的值。因?yàn)樗麄兲耍颠€沒來得及賦,就已經(jīng) alert 了。
插入圖片
若要演示此示例,最好是放在按鈕的事件處理程序中,目的是有些延遲,否則會說 FCKeditorAPI 未定義。
FCKeditorAPI.GetInstance("fckcontent").InsertHtml("<img src...>");
一般來說,我們在編輯內(nèi)容時,先是讀入到 textarea,再將 textarea 的內(nèi)容賦給編輯器。因?yàn)橹苯影褍?nèi)容作為字符串給編輯器的 Value 屬性賦值使用的是 JavaScript 代碼,要讓 JS 代碼不受內(nèi)容中雙引號、換行等的干擾,只有先讀入到 textarea 最方便。
使用 FCKeditor 2.6.5
復(fù)制代碼 代碼如下:
<div><textarea id="fckcontent" name="content">cftea</textarea></div>
<script type="text/javascript" src="fckeditor/fckeditor.js"></script>
<script type="text/javascript">
<!--
var oFCKeditor = new FCKeditor("fckcontent");
oFCKeditor.BasePath = "fckeditor/"; // fckeditor 文件夾位置。
oFCKeditor.Create();
//-->
</script>
本來應(yīng)該用 display:none 將 textarea 隱藏起來,不過為了查看效果,這里不隱藏。
這樣編輯器就自動與 fckcontent 關(guān)聯(lián)起來了,打開網(wǎng)頁時 FCKeditor 自動讀取 textarea 的內(nèi)容,提交時又自動將其內(nèi)容(自動為 XHTML)賦給 textarea。
注意,我們 textarea 的 id 和 name 值不一樣,為什么呢?
這里應(yīng)該是這個版本不太完善的地方,如果我們把 textarea 的 id 和 name 值設(shè)置為一樣,那么 FCKeditor 文本區(qū)的 name 值也是 content,在服務(wù)器端 Request.Form("content").Count 就會有兩個,我們服務(wù)器端取值就稍稍有點(diǎn)不方便,得用 Request.Form("content")(0)。如果我們將 id 設(shè)為 fckcontent,那么 FCKeditor 文本區(qū)的 name 就是 fckcontent,與 textarea 不同名。
設(shè)置編輯器寬高
var oFCKeditor = new FCKeditor("fckcontent", 500, 300);
或
復(fù)制代碼 代碼如下:
var oFCKeditor = new FCKeditor("fckcontent");
oFCKeditor.Width = 500;
oFCKeditor.Height = 300;
設(shè)置工具條
var oFCKeditor = new FCKeditor("fckcontent", 500, 300, "Basic");
注意第四個參數(shù),其可選值有 Basic、Default,注意大小寫不可搞錯,分別表示基本工具條、默認(rèn)工具條(全部按鈕)。
設(shè)置初始值、設(shè)置值、取值
設(shè)置初始值
實(shí)際上設(shè)置初始值很少用,因?yàn)橐话愣际桥c textarea 關(guān)聯(lián)的,故只是簡單列出來一下,不深究。說明一下,如果關(guān)聯(lián)的 textarea 存在,則賦初始值是沒有用的。
var oFCKeditor = new FCKeditor("fckcontent2", 500, 300, "Default", "腳本之家");
或
復(fù)制代碼 代碼如下:
var oFCKeditor = new FCKeditor("fckcontent2", 500, 300, "Default");
oFCKeditor.BasePath = "fckeditor/";
oFCKeditor.Value = "cftea"; // 必須在 Create 之前
oFCKeditor.Create();
設(shè)置值
若要演示此示例,最好是放在按鈕的事件處理程序中,目的是有些延遲,否則會說 FCKeditorAPI 未定義。
復(fù)制代碼 代碼如下:
var oEditor = FCKeditorAPI.GetInstance("fckcontent");
oEditor.SetHTML("腳本之家");
取值
若要演示此示例,最好是放在按鈕的事件處理程序中,目的是有些延遲,否則會說 FCKeditorAPI 未定義。
復(fù)制代碼 代碼如下:
var oEditor = FCKeditorAPI.GetInstance("fckcontent");
alert(oEditor.GetXHTML()); // 還有個類似方法是 GetHTML,但不推薦用 GetHTML。
您這樣做很危險:
復(fù)制代碼 代碼如下:
var oEditor = FCKeditorAPI.GetInstance("fckcontent");
oEditor.SetHTML("腳本之家");
alert(oEditor.GetXHTML()); // 這里的值并不一定是上一句賦的值。因?yàn)樗麄兲耍颠€沒來得及賦,就已經(jīng) alert 了。
插入圖片
若要演示此示例,最好是放在按鈕的事件處理程序中,目的是有些延遲,否則會說 FCKeditorAPI 未定義。
FCKeditorAPI.GetInstance("fckcontent").InsertHtml("<img src...>");
相關(guān)文章
FCKeditor 插件開發(fā) 示例(詳細(xì)版本)
2009-06-06Ueditor百度編輯器的Html模式自動替換樣式的解決方法
百度的Ueditor編輯器出于安全性考慮,用戶在html模式下粘貼進(jìn)去的html文檔會自動被去除樣式和轉(zhuǎn)義。雖然安全的,但是非常不方便。做一下修改把這個功能去掉,需要的朋友可以參考下2017-03-03最新版CKEditor的配置方法及插件(Plugin)編寫示例
本文記錄配置CKEditor過程,并以文章分頁插件為例,簡要CKEditor Plugin編寫過程。 從官網(wǎng)http://ckeditor.com/download下載最新版CKEditor,解壓2017-03-03SyntaxHighlighter 去掉右側(cè)滾動條的方法
SyntaxHighlighter這個是一個高亮插件。現(xiàn)在被用于很多網(wǎng)站的代碼顯示。但是SyntaxHighlighter3.0.83,由于自適應(yīng)寬和高,導(dǎo)致一直有滾動條的問題2020-03-03asp.net 為FCKeditor開發(fā)代碼高亮插件實(shí)現(xiàn)代碼
昨天已經(jīng)將BlogEngine的可視化編輯器換成了FCKeditor,作為一個程序員,在博客中插入代碼是很重要的一塊。網(wǎng)上現(xiàn)有的都是修改FCKeditor的fckeditorcode_gecko.js和fckeditorcode_ie.js以達(dá)到InsertCode的目的。這個方法非常麻煩,當(dāng)要使用FCKeditor新版本時都要重新修改這兩個文件,非常影響我們的效率。2008-08-08