UEditor是由百度WEB前端研发部开发的所见即所得的开源富文本编辑器,具备轻量、可定制、用户体验优秀等特色。javascript
官网连接html
进入到下载页面,选择相应的版本下载前端
这里咱们使用ASP.NET开发,因此选择.NET版本。java
将文件解压后,目录结构以下:web
将外部js引入到页面中json
<script src="Assets/js/ueditor/ueditor.config.js" type="text/javascript"></script> <script src="Assets/js/ueditor/editor_api.js" type="text/javascript"></script>
editor_api.js包含了全部的ueditor的外部引用连接。api
ueditor.config.js包含了ueditor相关的配置,咱们须要修改该配置文件中的参数服务器
// 服务器统一请求接口路径 ,serverUrl: URL + "./net/controller.ashx" //上传文件图片等服务端处理程序路径 //工具栏上的全部的功能按钮和下拉框,能够在new编辑器的实例时选择本身须要的从新定义 , toolbars: [[ 'fullscreen', 'source', '|', 'undo', 'redo', '|', 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|', 'rowspacingtop', 'rowspacingbottom', 'lineheight', '|', 'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|', 'directionalityltr', 'directionalityrtl', 'indent', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|', 'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|', 'simpleupload', 'insertimage', 'emotion', 'scrawl', 'insertvideo', 'music', 'attachment', 'map', 'gmap', 'insertframe', 'insertcode', 'webapp', 'pagebreak', 'template', 'background', '|', 'horizontal', 'date', 'time', 'spechars', 'snapscreen', 'wordimage', '|', 'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts', '|', 'print', 'preview', 'searchreplace', 'drafts', 'help' ]]
在./net/app_code路径下,ueditor已经帮咱们写好了一些经常使用的接口代码,如上传文件和图片app
咱们只须要在./net/config.json文件中对其进行一些设置就可使用webapp
打开config.json,修改上传图片配置项(主要修改路径前缀和保存图片命名格式)
"imageUrlPrefix": "/assets/js/ueditor/net/", /* 图片访问路径前缀 */ "imagePathFormat": "upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,能够自定义保存路径和文件名格式 */
上传文件的配置与其相似,就很少说了
还须要将editor_api.js文件中的路径修改成本身项目中的路径
baseURL = './Assets/js/ueditor/';
下面是html文件的代码
<div id="myEditor"></div>
而后是Js代码
var params = GetRequest(); //获取url参数 var editor = UE.getEditor('myEditor', { //这里能够选择本身须要的工具按钮名称,此处仅选择以下五个 toolbars: [['source', 'undo', 'redo', 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', '|', 'removeformat', 'formatmatch', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|', 'rowspacingtop', 'rowspacingbottom', 'lineheight', '|', 'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|', 'simpleupload', 'insertimage', 'emotion', 'scrawl', 'insertvideo', 'music', 'attachment']], //focus时自动清空初始化时的内容 autoClearinitialContent: true, //关闭字数统计 wordCount: false, //关闭elementPath elementPathEnabled: false, //默认的编辑区域高度 initialFrameHeight: 300 }) if (params.id != "" && params.id != undefined) { // editor准备好以后才可使用 ,否则不能使用setContent(),会报错 Cannot set property 'innerHTML' of undefined editor.addListener("ready", function () { $.get("./Handlers/NewsHandler.ashx", { action: "getnewsbyid", id: params.id }, function (data) { var json = $.parseJSON(data); $("#txtTitle").val(json.Title); $("#selType").val(json.Type); $("#selStyle").val(json.Style); console.log($.parseHTML(json.Content)[0].data); if ($.parseHTML(json.Content)[0].data) { editor.setContent($.parseHTML(json.Content)[0].data); } else { editor.setContent(json.Content); } }); }); }
附加一下经常使用的操做
获取UEditor内容的代码
var content = UE.getEditor('myEditor').getContent();
将后台获取的数据设置为UEditor的内容
// editor准备好以后才可使用 ,否则不能使用setContent(),会报错 Cannot set property 'innerHTML' of undefined editor.addListener("ready", function () { $.get("./Handlers/NewsHandler.ashx", { action: "getnewsbyid", id: params.id }, function (data) { var json = $.parseJSON(data); $("#txtTitle").val(json.Title); $("#selType").val(json.Type); $("#selStyle").val(json.Style); console.log($.parseHTML(json.Content)[0].data); if ($.parseHTML(json.Content)[0].data) { editor.setContent($.parseHTML(json.Content)[0].data); } else { editor.setContent(json.Content); } }); });
下面是运行效果