最近用layui搭一个后台, 在发文章的时候有用到富文本编辑器 ,这里记录一下.
本人用的是 kindeditor富文本编辑器,因为layui自带的富文本感觉功能不是很多.
kindeditor 下载地址 : http://kindeditor.net/down.php
从官网下下来的 kindeditor 文件夹里取我们需要用到的几个目录就行了
直接放到项目静态资源文件里 就行了
在页面里引入本地富文本的js文件 因为我用的是 springboot 所以静态资源路径直接用根路径就行了.
<link rel="stylesheet" href="/kindeditor/themes/default/default.css" />
<link rel="stylesheet" href="/kindeditor/plugins/code/prettify.css" />
<script charset="utf-8" src="/kindeditor/kindeditor-all.js"></script>
<script charset="utf-8" src="/kindeditor/lang/zh-CN.js"></script>
<script charset="utf-8" src="/kindeditor/plugins/code/prettify.js"></script>
然后再对应的渲染富文本编辑器. 我把这一段放在 <head> 标签里了, 因为在layui的弹出层里可能会因为延迟加载不出来,所以直接放在head里加载.
<script>
var editor1;
KindEditor.ready(function(K) {
editor1 = K.create('textarea[name="content"]',{
//参数配置
width : '95%',
filePostName: "file",
uploadJson: '/fileupload/uploadArticleImg',
minHeight: '600',
resizeType : 0,//禁止拉伸,1可以上下拉伸,2上下左右拉伸
filterMode: false,//true时过滤HTML代码,false时允许输入任何代码。
itmes: [
'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',
'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage',
'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
'anchor', 'link', 'unlink', '|', 'about'
]
}
);
// prettyPrint();
});
</script>
其中
uploadJson: '/fileupload/uploadArticleImg',是富文本里面上传图片的接口
textarea[name="content"] 对应 需要渲染成富文本的那个 textarea标签
然后就好啦
获取富文本的内容 只需要
editor1.html() 即可
editor1.html(" 给富文本赋值") 也可以这样给富文本动态赋值.