【前端】wangEditor(富文本编辑器) 简易使用示例

 

转载请说明作者或者注明出处,谢谢

 

说到前端常用的编辑器,自然也少不了富文本编辑器(RichText Editor)

笔者在此之前也看了一些相关的在线编辑器,其中包括了当百度搜索“富文本编辑器”字样时出现在最上面的百度UEditor,里面的功能相当丰富,但是配置也相对复杂,对于使用者要求较高。况且在轻量级网页应用的开发中也并不需要如此多的功能,所以真正需要的是一个使用简单的轻量级富文本编辑器,出于方便考虑,也应该尽可能保证“所见即所得”原则。

wangEditor正是其中之一(官网链接:wangEditor

另外值得一提的是,该editor的官网所提供的demo以及入门文档写的十分友好,稍有前端经验的代码手即可快速配置上手。好了废话不多说,上我自己的源代码:

 1 <html>
 2     <head>
 3         <!--在这里字符集的设定很重要,如果设定不当将会出现乱码-->
 4         <meta charset="UTF-8">
 5         <title>wangEditor demo</title>
 6     </head>
 7     <body>
 8         <!--wangEditor是一款基于jquery框架开发的插件-->
 9         <script src="http://cdn.bootcss.com/wangeditor/2.1.20/js/lib/jquery-2.2.1.js"></script>
10     
11         <!--编辑器位置-->
12         <div style="max-width:700px;margin:50px">
13             <div id="txtdiv" style="border:1px solid gray;min-height:240px">
14             
15             </div>
16         </div>
17         
18         <!--效果展示框-->
19         <div id="show_box" style="border: 1px solid gray;margin-left:50px"></div>
20         
21         <!--脚本控制-->
22         <script>
23             $(function(){
24                 //初始化编辑器
25                 editor = new wangEditor("txtdiv");
26                 editor.create();
27                 
28                 //内容修改事件,此处做的是实时展示实际效果
29                 editor.onchange = function(){
30                     //获取editor的html值
31                     var html = editor.$txt.html();
32                     $("#show_box").html(html)
33                 }
34             })
35         </script>
36     
37         <!--按照官网上的说明,js和css的这两个引用应该放在body的末尾-->
38         <script src="http://cdn.bootcss.com/wangeditor/2.1.20/js/wangEditor.js"></script>
39         <link href="http://cdn.bootcss.com/wangeditor/2.1.20/css/wangEditor.css" rel="stylesheet">
40     </body>
41 </html>

 

效果图:

 

posted @ 2017-03-16 14:24  HansBug  阅读(23454)  评论(3编辑  收藏  举报