当前位置: 移动技术网 > IT编程>脚本编程>编辑器 > 使用Js获取、插入和更改FCKeditor编辑器里的内容

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

2020年05月12日  | 移动技术网IT编程  | 我要评论

之前在一个系统里使用了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编辑器内容请搜素移动技术网以前的文章或下面相关文章,希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网