FCKeditor(現更名為 CKEditor 4/5)是經典的開源富文本編輯器,支援 IE6+/Firefox/Safari/Opera,輸出 XHTML 相容代碼,提供工具列、圖片上傳、樣式下拉、拼寫檢查等完整功能。本文基於 FCKeditor 2.x 架構說明整合細節。
FCKeditor 的資料夾(名稱固定,因配置檔預設路徑硬編碼)。https://your-site/FCKeditor/_samples/default.html 查看示範頁。fckconfig.js 中的 FCKConfig.BasePath 參數(如 /CustomPath/fckeditor/),結尾必須帶斜線。底線開頭資料夾/檔案(_samples, _source, _test 等)為開發/測試用,部署時可安全刪除。<script type="text/javascript" src="/FCKeditor/fckeditor.js"></script>
<form>
<script type="text/javascript">
var oFCKeditor = new FCKeditor('FCKeditor1');
oFCKeditor.Create();
</script>
</form>
<script type="text/javascript">
window.onload = function() {
var oFCKeditor = new FCKeditor('MyTextarea');
oFCKeditor.ReplaceTextarea();
};
</script>
<textarea id="MyTextarea" name="MyTextarea">初始內容</textarea>
| 參數/屬性 | 說明 | 預設值 |
|---|---|---|
| instanceName | 編輯器唯一實例名(相當於 ID) | 必填 |
| width | 寬度(像素/百分比) | 100% |
| height | 高度(像素) | 200 |
| toolbarSet | 工具列集合名稱(定義於 fckconfig.js) | Default |
| value | 初始化 HTML 內容 | 空字串 |
| BasePath | 編輯器基底路徑(建議站點根目錄相對路徑,以 / 結尾) | /FCKeditor/ |
| CheckBrowser | 啟動前檢查瀏覽器相容性 | true |
| DisplayErrors | 顯示錯誤提示 | true |
| Config[Key] | 動態覆寫配置項(如 oFCKeditor.Config['DefaultLanguage']='zh-cn') | - |
Create():建立並輸出編輯器 HTML。ReplaceTextarea(textareaName):以編輯器取代指定 name 的 textarea。AutoDetectLanguage:自動偵測語言 (true/false)BaseHref:相對連結基底網址ContentLangDirection:預設文字方向 (ltr/rtl)ContextMenu:右鍵選單項目陣列CustomConfigurationsPath:自定義配置檔路徑Debug:開啟除錯模式DefaultLanguage:預設語言代碼EditorAreaCSS:編輯區域樣式表檔案EnableSourceXHTML / EnableXHTML:輸出 XHTML 而非 HTMLFillEmptyBlocks:空區塊填入 FontColors/FontFormats/FontNames/FontSizes:字體/大小/格式/顏色下拉選單內容ForcePasteAsPlainText:強制貼上為純文字ImageBrowser / LinkBrowser:啟用圖片/連結瀏覽伺服器功能ImageBrowserURL / LinkBrowserURL:瀏覽伺服器的後端處理網址Plugins / PluginsPath:外掛註冊與路徑SkinPath:皮膚資料夾位置SmileyImages / SmileyPath / SmileyColumns:表情符號設定SpellChecker:拼寫檢查器 (ieSpell/SpellerPages)StartupFocus:啟動時聚焦編輯器StylesXmlPath:XML 定義樣式下拉清單檔案位置ToolbarCanCollapse / ToolbarStartExpanded:工具列摺疊/展開行為UseBROnCarriageReturn:Enter 產生 <br> 而非 <p>/<div><?xml version="1.0" encoding="utf-8"?>
<Styles>
<Style name="My Image" element="img">
<Attribute name="style" value="padding: 5px" />
<Attribute name="border" value="2" />
</Style>
<Style name="Italic" element="em" />
<Style name="Title" element="span">
<Attribute name="class" value="Title" />
</Style>
<Style name="Title H3" element="h3" />
</Styles>
每個 <Style> 定義一個樣式:name 顯示在下拉選單,element 限定適用標籤(上下文敏感,選取不同元素顯示不同可用樣式)。
fckconfig.js 中 FCKConfig.SpellChecker = 'SpellerPages'; 並設定對應 URL。FCKeditor 採用移除註解/空白、縮短變數名、合併檔案等方式壓縮核心腳本,減少下載體積。生產環境請使用壓縮版 (fckeditor.js 已壓縮),開發除錯可用 _source 目錄下原始碼。
FCKeditor 已停止維護(2009 年末釋出 2.6.6 為最終版)。現代專案請遷移至: