其開源協議是基于 GPL, LGPL 和 MPL 的。官方網站:http://ckeditor.com/
一般來說,我們在編輯內容時,先是讀入到 textarea,再將 textarea 的內容賦給編輯器。因為直接把內容作為字符串給編輯器的 Value 屬性賦值使用的是 JavaScript 代碼,要讓 JS 代碼不受內容中雙引號、換行等的干擾,只有先讀入到 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>
本來應該用 display:none 將 textarea 隱藏起來,不過為了查看效果,這里不隱藏。
這樣編輯器就自動與 fckcontent 關聯起來了,打開網頁時 FCKeditor 自動讀取 textarea 的內容,提交時又自動將其內容(自動為 XHTML)賦給 textarea。
注意,我們 textarea 的 id 和 name 值不一樣,為什么呢?
這里應該是這個版本不太完善的地方,如果我們把 textarea 的 id 和 name 值設置為一樣,那么 FCKeditor 文本區的 name 值也是 content,在服務器端 Request.Form("content").Count 就會有兩個,我們服務器端取值就稍稍有點不方便,得用 Request.Form("content")(0)。如果我們將 id 設為 fckcontent,那么 FCKeditor 文本區的 name 就是 fckcontent,與 textarea 不同名。
設置編輯器寬高
var oFCKeditor = new FCKeditor("fckcontent", 500, 300);
或
復制代碼 代碼如下:
var oFCKeditor = new FCKeditor("fckcontent");
oFCKeditor.Width = 500;
oFCKeditor.Height = 300;
設置工具條
var oFCKeditor = new FCKeditor("fckcontent", 500, 300, "Basic");
注意第四個參數,其可選值有 Basic、Default,注意大小寫不可搞錯,分別表示基本工具條、默認工具條(全部按鈕)。
設置初始值、設置值、取值
設置初始值
實際上設置初始值很少用,因為一般都是與 textarea 關聯的,故只是簡單列出來一下,不深究。說明一下,如果關聯的 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();
設置值
若要演示此示例,最好是放在按鈕的事件處理程序中,目的是有些延遲,否則會說 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()); // 這里的值并不一定是上一句賦的值。因為他們太近了,值還沒來得及賦,就已經 alert 了。
插入圖片
若要演示此示例,最好是放在按鈕的事件處理程序中,目的是有些延遲,否則會說 FCKeditorAPI 未定義。
FCKeditorAPI.GetInstance("fckcontent").InsertHtml("img src...>");