最近用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(" 給富文本賦值") 也可以這樣給富文本動態賦值.