使用Js获取、插入和更改FCKeditor编辑器里的内容

所属分类: 网络编程 / 网页编辑器 阅读数: 1084
收藏 0 赞 0 分享

之前在一个系统里使用了FCKeditor编辑器,由于项目需求需要在FCKeditor里添加一个自定义的按钮用于实现自己的需求

主要是在点击该按钮时删除或添加FCKeditor编辑器里的内容

其实是一个很简单的需求,本来以为在FCKeditor可以很容易的实现
在Google上搜索自定义按钮,插件开发,经过近二个小时的摸索最终还是没有实现不知是我太笨还是自定义插件太难啦

通过JS方式来处理

1.在页面中添加checkbox元素并绑定事件,选中该元素时将在FCKeditor内容里添加"{#book#}"字符串(该字符串会在适当的时候被替换成其他内容),取消选中时则删除

<label><input type="checkbox" id="lineBook" onclick="chk_but();"/>添加/删除复选框</label>

2.添加Js处理FCKeditor内容(添加或删除"{#book#}"字符串),'txtContent'为FCKeditor的ID控控件ID

<script type = "text/javascript" >
//"添加/删除复选框"点击时如果按钮选中则添加"{#book#}"字符串到FCK内容里,反之删除字符串
//lineBook为FCK的ID号
function chk_but() {
  if (window.FCKeditorAPI !== undefined && FCKeditorAPI.GetInstance('txtContent') !== undefined) {
    if (document.getElementById('lineBook').checked) {
      FCKeditorAPI.GetInstance('txtContent').EditorDocument.body.innerHTML += "{#book#}";
    } else {
      FCKeditorAPI.GetInstance('txtContent').EditorDocument.body.innerHTML = FCKeditorAPI.GetInstance('txtContent').EditorDocument.body.innerHTML.replace("{#book#}", "");
    }
  }
} //end function chk_lineBook()
//内容里如果有{#book#}则选中"添加/删除复选框"
if (document.getElementById('txtContent').value.indexOf('{#book#}') >= 0 
  && window.FCKeditorAPI !== undefined 
  && FCKeditorAPI.GetInstance('txtContent') !== undefined) {
  document.getElementById('lineBook').checked = true;
} 
</script>

参考:

官网:http://ckeditor.com/

获取或更改内容值:http://bbs.csdn.net/topics/360086762

创建插件:http://docs.cksource.com/FCKeditor_2.x/Developers_Guide/Customization/Plug-ins

接着给大家分享一下JS操作Fckeditor的一些常用方法

//向编辑器插入指定代码 
function insertHTMLToEditor(codeStr){ 
 var oEditor = FCKeditorAPI.GetInstance("content");
 oEditor.InsertHtml(codeStr); // "html"为HTML文本
}
//获取编辑器中HTML内容
function getEditorHTMLContents() {
 var oEditor = FCKeditorAPI.GetInstance("content");
 return(oEditor.GetXHTML(false));
}
// 获取编辑器中文字内容
function getEditorTextContents() {
 var oEditor = FCKeditorAPI.GetInstance("content");
 return(oEditor.EditorDocument.body.innerText);
}
// 设置编辑器中内容
function SetEditorContents(ContentStr) {
 var oEditor = FCKeditorAPI.GetInstance("content") ;
 oEditor.SetHTML(ContentStr) ;
}
//向编辑器插入指定代码 
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() ; 
}

到此这篇关于使用Js获取、插入和更改FCKeditor编辑器里的内容的文章就介绍到这了,更多相关Js操作FCKeditor编辑器内容请搜素脚本之家以前的文章或下面相关文章,希望大家以后多多支持脚本之家!

更多精彩内容其他人还在看

关于CKeditor的非主流个性应用的设置

因为我的网站需要一个编辑器,所以用周末时间研究了一下CKeditor,终于最终修改成了合适的样子。
收藏 0 赞 0 分享

dedecms5.5 最新版ckeditor编辑器整合教程

CKEditor是fckeditor的3.0版。经过近两年的紧张开发,第一个CKEditor 3.0稳定版本终于发布。
收藏 0 赞 0 分享

javascript 网页编辑框及拖拽图片的问题

javascript 网页编辑框及拖拽图片的问题,需要的朋友可以参考下。
收藏 0 赞 0 分享

asp.net FCKeditor 2.6.3 配置说明

FCKeditor是一个功能强大支持所见即所得功能的文本编辑器,可以为用户提供微软office软件一样的在线文档编辑服务。
收藏 0 赞 0 分享

轻松实现 CKfinder进驻CKEditor3.0

看完YaSin写得“将CKfinder 整合进 CKEditor3.0 ”之后,我总觉得问题有点复杂,尤其是修改压缩了的js文件。
收藏 0 赞 0 分享

将CKfinder 整合进 CKEditor3.0的方法

CKFinder是一款基于AJAX的文件浏览器,这是ASP.NET专用版,它可以在线浏览文件、管理文件、上传文件,以树形Tree的方式展开目录,自动检测图片并生成缩略图,它是由Fckeditor公司出品,同时也可配合FckEditor来使用,可达到意想不到的效果。
收藏 0 赞 0 分享

ASp.net下fckeditor配置图片上传最简单的方法

fckeditor 的基本配置我就不讲了,讲讲图片上传这块吧;
收藏 0 赞 0 分享

ckeditor 简单配置方法

ckeditor 简单配置使用方法
收藏 0 赞 0 分享

asp.net CKEditor和CKFinder的应用

CKEditor和CKFinder在ASP.NET中的应用,需要的朋友可以参考下。
收藏 0 赞 0 分享

FCKEDITOR 的高级功能和常见问题的解决方法

FCKeditor是一个专门使用在网页上属于开放源代码的所见即所得文字编辑器。它志于轻量化,不需要太复杂的安装步骤即可使用。
收藏 0 赞 0 分享
查看更多