1:官方下载KindEditor插件:http://kindeditor.net/down.php
2:页面引入两个js和一个css文件
/kindeditor/kindeditor-all.js
/kindeditor/lang/zh-CN.js
/kindeditor/themes/default/default.css
3:页面定义textarea
<textarea id="editor_id" name="content" >
</textarea>
4:js创建富文本
KindEditor.ready(function(K) {
//全局配置
var serverScriptPath=basePath+'static/common/kindeditor/jsp/';//服务端文件文件夹URL绝对路径,最后要有/
var editorPluginsPath=basePath+'static/common/kindeditor/plugins/';//编辑器的插件文件夹URL绝对路径,最后要有/
//全局配置结束
var editor1 = K.create('#editor_id', {//指定textarea
uploadJson : serverScriptPath+'upload_json.jsp',//上传文件处理方法,记得修改该文件里的路径,否则上传时会报找不到存储路径,我这里是jsp页面引用所以选择.jsp,根据不同的语言技术选择对应的处理文件
fileManagerJson : serverScriptPath+'file_manager_json.jsp',//跟上面一样处理
cssPath :editorPluginsPath+'code/prettify.css',
emoticonsPath:editorPluginsPath+"/emoticons/images/",
//不要改动结束
allowImageUpload:true,//允许上传图片
allowFileManager:true, //允许对上传图片进行管理
afterBlur: function(){ //利用该方法处理当富文本编辑框失焦之后,立即同步数据
KindEditor.sync("#editor_id") ;
}
});
});
5:保存富文本内容
$('#submitBtn').click(function(){
//获取富文本内容
var fwbContent = $('#editor_id').val();
var params = {fwbContent:fwbContent};
$.ajax({
type: "POST",
url: "fwb/dosave",
contentType : "application/json",
data: JSON.stringify(params),
dataType : "json",
success: function(res){
if(res.code == 0){
layer.open({
title: '消息',content: '保存成功!'
}else{
layer.open({
title: '消息',content: '保存失败!'
});
}
}
});
});
6:页面展示修改富文本内容,需要在触发修改按钮再创建KindEditor
KindEditor.create('#content', {//指定textarea
uploadJson : serverScriptPath+'upload_json.jsp',
fileManagerJson : serverScriptPath+'file_manager_json.jsp',
cssPath :editorPluginsPath+'code/prettify.css',
emoticonsPath:editorPluginsPath+"/emoticons/images/",
//不要改动结束
allowImageUpload:true,//允许上传图片
allowFileManager:true, //允许对上传图片进行管理
afterBlur: function(){ //利用该方法处理当富文本编辑框失焦之后,立即同步数据
KindEditor.sync("#content") ;
}
});
KindEditor.html('#content',data.content);//加载要展示的数据
7:KindEditor兼容layui有点问题需要将kindeditor-all.js里的所有811213替换成19892001