在FCKeditor的开发中,经常需要与编辑器实例进行交互,比如获取或设置其内容。下面这三个函数就是最常用的API调用,直接复制到你的项目里就能用,省去每次手写重复代码的麻烦。

function getEditorHTMLContents(EditorName) 
{
    var oEditor = FCKeditorAPI.GetInstance(EditorName);
    return(oEditor.GetXHTML(true));
}

function getEditorTextContents(EditorName) 
{
    var oEditor = FCKeditorAPI.GetInstance(EditorName);
    return(oEditor.EditorDocument.body.innerText);
}

function SetEditorContents(EditorName, ContentStr) 
{
    var oEditor = FCKeditorAPI.GetInstance(EditorName) ;
    oEditor.SetHTML(ContentStr) ;
}

三个函数分别对应:获取HTML内容、获取纯文本内容、设置编辑器内容。参数EditorName就是你在页面上初始化FCKeditor时指定的那个实例名称。注意getEditorHTMLContents返回的是经过XHTML规范化的代码,而getEditorTextContents直接取innerText,会丢掉所有HTML标签。设置内容时用SetHTML,直接传入字符串即可。

本文转载于:https://www.jb51.net/article/23649.htm 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。