最佳答案
直接通过FckEditorAPI获取编辑器实例,然后修改:
准备条件:执行初始化代码:
<script type="text/javascript">
window.onload = function(){
var oFCKeditor = new FCKeditor('FCKmessage') ;
oFCKeditor.BasePath = "/fckeditor/";
oFCKeditor.Width="100%";
oFCKeditor.Height="400";
oFCKeditor.ReplaceTextarea();
}
</script>
如何取值?取值相对比较简单:
var content = FCKeditorAPI.GetInstance("FCKmessage").GetXHTML("true");
如何赋值?赋值的情况相对比较复杂。这是因为FCKEditor编辑器有两种模式,一种是源代码模式,一种是可视化编辑模式。这两种模式赋值的情况是不一样的。
实现代码如下,代码实际没这么复杂,可以参考简化实现。
<script type="text/javascript">
function SetFCKEditor(htmlStr){
if (typeof(FCKeditorAPI) == 'undefined') {
return false;
}
var oEditor =FCKeditorAPI.GetInstance("FCKmessage");
if(oEditor.EditorDocument!=null){
htmlStr = htmlStr.replace(/\<br \/\>\r\n/gi, '\r\n');
htmlStr = htmlStr.replace(/\<br \/\>\n/gi, '\n');
htmlStr = htmlStr.replace(/\r\n/gi, "<br />");
htmlStr = htmlStr.replace(/\n/gi, "<br />");
oEditor.EditorDocument.body.innerHTML = htmlStr;
}
else if (typeof(oEditor.EditingArea) != 'undefined'
&& typeof(oEditor.EditingArea.Textarea) != 'undefined'){
//textStr is more suitable compared with htmlStr
htmlStr = htmlStr.replace(/\<br \/\>\r\n/gi, '\r\n');
htmlStr = htmlStr.replace(/\<br \/\>\n/gi, '\n');
htmlStr = htmlStr.replace(/\r\n/gi, "<br />");
htmlStr = htmlStr.replace(/\n/gi, "<br />");
oEditor.EditingArea.Textarea.innerText = htmlStr;
}
currentTab="FCKEditor";
}
</script>
简化模式
<script type="text/javascript">
function SetFCKEditor(htmlStr){
if (typeof(FCKeditorAPI) == 'undefined') {
return false;
}
var oEditor =FCKeditorAPI.GetInstance("FCKmessage");
if(oEditor.EditorDocument!=null){
oEditor.EditorDocument.body.innerHTML = htmlStr;
}
else if (typeof(oEditor.EditingArea) != 'undefined'
&& typeof(oEditor.EditingArea.Textarea) != 'undefined'){
//textStr is more suitable compared with htmlStr
oEditor.EditingArea.Textarea.innerText = htmlStr;
}
currentTab="FCKEditor";
}
</script>
调用:
var htmlStr = "<span>我要重新设置的内容</span>";
SetFCKEditor(htmlStr);
回答时间:2010-7-23 20:21:08
| 回答者:
56max