DIV CSS 佈局教程網

JavaScript 讀取 FCK 編輯器內容的具體方法
編輯:JavaScript綜合知識     

FCKeditor 是曾經相當流行的所見即所得網頁編輯器。若要在自訂功能中存取它的內容,可以透過 FCKeditorAPI 來操作。以下整理幾個常用做法。

取得編輯器內容

function getEditorContents(){
    var oEditor = FCKeditorAPI.GetInstance("content");
    alert(oEditor.GetXHTML(true));
}

向編輯器插入程式碼

function insertHTMLToEditor(codeStr){
    var oEditor = FCKeditorAPI.GetInstance("content");
    if (oEditor.EditMode == FCK_EDITMODE_WYSIWYG){
        oEditor.InsertHtml(codeStr);
    } else {
        return false;
    }
}

統計內容字數

function getLength(){
    var oEditor = FCKeditorAPI.GetInstance("content");
    var oDOM = oEditor.EditorDocument;
    var iLength;
    if (document.all){
        iLength = oDOM.body.innerText.length;
    } else {
        var r = oDOM.createRange();
        r.selectNodeContents(oDOM.body);
        iLength = r.toString().length;
    }
    alert(iLength);
}

執行指定命令

function ExecuteCommand(commandName){
    var oEditor = FCKeditorAPI.GetInstance("content");
    oEditor.Commands.GetCommand(commandName).Execute();
}

設定編輯器內容

function SetContents(codeStr){
    var oEditor = FCKeditorAPI.GetInstance("content");
    oEditor.SetHTML(codeStr);
}

其中 GetInstance("content") 的參數是編輯器實例的名稱,需與實際建立時一致。透過這些方法,就能在自訂的表單驗證、字數限制、內容預填等情境中靈活操作 FCKeditor。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved