ThinkPHP示例:强大的Ueditor富文本编辑器

jerry thinkphp 2015年11月18日 收藏
ThinkPHP示例:强大的Ueditor富文本编辑器



于2013-09-10更新版本:
Ueditor文本编辑器 - 完整demo
http://www.thinkphp.cn/code/325.html




具体插件下载:
http://ueditor.baidu.com/website/download.html#ueditor

解压到:PUBLIC/Ueditor下(同级目录有:Common,Conf,Lib,Tpl等)

例:在Tpl/model/model.html :
  1. <html>
  2. <title>Ueditor文本编辑器</title>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <load href="/Public/Js/Jquery/jquery-1.7.2.js" />
  6. <script type="text/javascript">
  7. window.UEDITOR_HOME_URL = "/ThinkPHP/Public/Ueditor/";    //UEDITOR_HOME_URL、config、all这三个顺序不能改变(绝对路径)
  8. </script>
  9. <load href="/Public/Ueditor/editor_config.js" />
  10. <load href="/Public/Ueditor/editor_all_min.js" />

  11. <script type="text/javascript">
  12. var editor;

  13. function BeForeForm(formName){

  14.     if(editor.hasContents()){ //此处以非空为例
  15.     
  16.         editor.sync();       //同步内容

  17.         $("form[name='"+formName+"']").submit();   //提交表单判断,此方法是自己写的,不予给出,抱歉!只给提交方法!
  18.     }
  19. }
  20. </script>
  21. </head>
原本富文本域写法如下:
<textarea></textarea>
现在Ueditor支持div,script等标签 建议:script,因为这是官方文档的建议如:
  1. <body>
  2. <form name='MyForm' id='MyForm' method='POST' action="">
  3. <script type="text/plain" id="content" name="content"></script>
  4. <script type="text/javascript">
  5. $(function(){
  6.     var editor;
  7.         //具体参数配置在  editor_config.js  中
  8.     var options = {
  9.         initialFrameWidth:1000,        //初化宽度
  10.         initialFrameHeight:300,        //初化高度
  11.         focus:false,                        //初始化时,是否让编辑器获得焦点true或false
  12.         maximumWords:1000,        //允许的最大字符数
  13.     };
  14.     editor = new UE.ui.Editor(options);
  15.     editor.render("content");
  16.     editor.ready(function(){
  17.         editor.setContent('{$vo.content}');     //加载数据库Action.class.PHP传过来的值
  18.     });
  19. });    
  20. </script>

  21. <input type="button" value="保存"  onclick="BeForeForm('MyForm');"> 
  22. </form>
  23. </body>
  24. </html>
后台表单提交后的Action.class.php 原本ThinkPHP的写法存入数据库便可!可无需类似 $content = htmlspecialchars($_POST['content']); 该方法的转换
备注:content 字段是为 text 类型!

后台代码为:
  1. $direction = D("direction");  //包含 content 字段
  2. $direction -> create();
  3. $direction -> add();