要在網頁中嵌入 FCKeditor 這套所見即所得的線上編輯器,其實只需要幾個簡單的步驟。以下示範如何在頁面中正確載入並初始化它。
首先在頁面中載入 FCKeditor 的主程式檔案:
<script type="text/javascript" src="editor/fckeditor.js"></script>
接著撰寫一段 JavaScript,建立編輯器實例並設定相關參數:
<script type="text/javascript">
var oFCKeditor = new FCKeditor('zhangthree');
oFCKeditor.BasePath = '/editor/';
oFCKeditor.Height = 300;
oFCKeditor.Value = '隨便寫點內容!';
oFCKeditor.Create();
</script>
在官方範例的原始碼中,有一段自動計算編輯器基底路徑的邏輯,它是根據 _samples 目錄的位置來推算的,僅適合示範用途。在真正的專案裡,建議直接明指定義 BasePath:
// 僅供範例使用,正式環境請直接指定 // oFCKeditor.BasePath = '/fckeditor/';
各參數的意義如下:
/fckeditor/。第一個參數 'zhangthree' 是這個編輯器對應的表單欄位名稱,當表單送出時,使用者輸入的內容就會以這個名稱作為 key 一併提交到後端。