如何在网页打开后,FckEditor已经初始化之后,再对FckEditor中的内容赋值或者修改?
[已解决] 如何在网页打开后,FckEditor已经初始化之后,再对FckEditor中的内容赋值或者修改?
当前页面:http://www.senparc.com/SZD-163
{ 收藏当前页面 }
最佳答案
直接通过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);
准备条件:执行初始化代码:
<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
其他参考答案(0)
提交失败!请检查错误!错误信息:
以下信息或许对您有用:
- [已解决] 20 如何用 javascript 获取当页面上鼠标(光标)在文本框中的位置? 2011/7/5 18:11:41
- [已解决] 20 javascritp对fckeditor编辑器赋值取值,以及内部机制说明? 2010/7/23 19:57:31
- [已解决] 30 如何判断javascript中的某个方法是否存在? 2010/2/21 16:25:53
- [已解决] 30 比较一下目前比较流行的js框架? 2009/11/1 23:00:33