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。