HTML编辑器UEditor的简单使用

编程知识 行业动态 更新时间:2024-06-13 00:17:28

参考自:http://ueditor.baidu/website/document.html


关于HTML编辑器,试过FCKeditor,升级版的CKeditor,还有TinyMCE,最近在尝试使用百度的UEditor。对比一下还是觉得UEditor的配置较简单,上手快并且文档和例子也很齐全。那么这里以UEditor1.2.3.0PHP版本UTF-8版为例梳理一下UEditor的使用流程。


1.首先是UEditor的文档结构



_examples:编辑器各种版本的示例页面

_src:JS文件

dialogs:弹出对话框对应的资源和JS文件

lang:语言包文件

PHP:文件上传处理,远程图片抓取,图片在线管理,屏幕截图相关文件

themes:样式图片和样式文件

third-party:第三方插件

editor_all.js:_src目录下所有文件的打包文件

editor_all_min.js:editor_all.js文件的压缩版,可以在正式部署时才采用

editor_config.js:编辑器的配置文件


2.系统配置

UEditor的配置可以分为系统默认配置和用户自定义配置两种类型。系统默认配置分散在各个对应的核心或者插件文件之中,对用户不可见。当用户注释掉自定义配置时起作用。用户自定义配置包括两种类型,一种位于editor_config.js文件之中,优先级高于系统默认配置;另一种位于实例化编辑器时传入的参数中,优先级最高。默认情况下,UEditor在editor_congfig.js注释掉了所有可以省略的配置项,采用系统默认配置,若取消注释,则以该配置项为准;未注释的配置项要求用户必需按照项目实际填写。

以下是自己定义的一个简单配置:

[html]   view plain  copy
  1. <script type="text/javascript">  
  2. // 自定义的编辑器配置项,此处定义的配置项将覆盖editor_config.js中的同名配置  
  3.  var editorOption = {  
  4.  //这里可以选择自己需要的工具按钮名称,此处仅选择如下四个  
  5.  toolbars:[['FullScreen', 'Source', 'Undo', 'Redo']]  
  6. ​ //更多其他参数,请参考editor_config.js中的配置项  
  7. };  
  8. </script>  

其中最重要的配置是第28行关于URL参数的配置,关系到图片上传,处理等路径,需要配置为uediotr在网站的相对路径或者绝对路径。如我的项目名称为t,根目录为www,则URL的值设置如下:

[html]   view plain  copy
  1. URL = window.UEDITOR_HOME_URL||"/t/UEditor/";  

3.引入配置文件,JS文件和主题CSS文件

[html]   view plain  copy
  1. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  2. <script type="text/javascript" src="UEditor/editor_config.js"></script>  
  3. <script type="text/javascript" src="UEditor/editor_all.js"></script>  
  4. <link rel="stylesheet" href="UEditor/themes/default/ueditor.css">  

4.创建编辑器实例及其DOM容器

[html]   view plain  copy
  1. <form action="" method="post" name="myForm">  
  2. <!--以下引入UEditor编辑器界面-->  
  3. <script type="text/plain" id="editor" name="myContent"></script>  
  4.  //此处不设置name,则editor默认名字为"editorValue",可以在  
  5. //editor_config.js中配置参数textarea,或者在此处设置  
  6. <input type="submit" name="submit" value="upload"/>  
  7. </form>  


5.设置选项并且渲染编辑器

如果需要有不同设置的ueditor,可以分别定义之后设置不同的自定义选项再渲染编辑器,

[html]   view plain  copy
  1. <script type="text/plain" id="myEditor" style="width:800px"></script>  
  2. <script type="text/plain" id="myEditor1" style="width:800px"><p>这里我可以写一些输入提示</p>  
  3. </script>  
  4. <script type="text/javascript">  
  5. var editor_a = new baidu.editor.ui.Editor();  
  6. editor_a.render( 'myEditor' ); //此处的参数值为编辑器的id值  
  7. var editor_a1 = new baidu.editor.ui.Editor({  
  8. //这里可以选择自己需要的工具按钮名称,此处仅选择如下五个  
  9. toolbars:[['FullScreen', 'Source', 'Undo', 'Redo','Bold']]  
  10. //更多其他参数,请参考editor_config.js中的配置项  
  11. });  
  12. editor_a1.render( 'myEditor1' );  
  13. </script>  

6.前后台数据交互

对于PHP可以直接用$_POST['ueditorName']获取编辑器的值,其中ueditorName的值为初始化编辑器时ueditor的name值。

以下为完整的渲染2个ueditor的代码

[html]   view plain  copy
  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>UEditor</title>  
  6. <!--以下为引入UEditor资源部分-->  
  7. <script type="text/javascript" src="UEditor/editor_config.js"></script>  
  8. <script type="text/javascript" src="UEditor/editor_all.js"></script>  
  9. <link rel="stylesheet" href="UEditor/themes/default/ueditor.css">  
  10. <!--以上为引入UEditor资源部分-->  
  11. </head>  
  12. <body>  
  13. <form action="test.php" method="post" name="myForm">  
  14. <!--以下引入UEditor编辑器界面-->  
  15. <script type="text/plain" id="myEditor" name="myContent">这是一个测试还是一个测试</script>  
  16. <input type="submit" name="submit" value="upload"/>  
  17. </form>  
  18. <script type="text/plain" id="myEditor1"><p>这里我可以写一些输入提示</p></script>  
  19. <script type="text/javascript">  
  20.     var editor_a = new baidu.editor.ui.Editor();  
  21.     editor_a.render( 'myEditor' ); //此处的参数值为编辑器的id值  
  22.       
  23.     var editor_a1 = new baidu.editor.ui.Editor({  
  24.     //这里可以选择自己需要的工具按钮名称,此处仅选择如下五个  
  25.     toolbars:[['FullScreen', 'Source', 'Undo', 'Redo','Bold']],  
  26.     //focus时自动清空初始化时的内容  
  27.     autoClearinitialContent:true,  
  28.     //更多其他参数,请参考editor_config.js中的配置项  
  29. });  
  30.     editor_a1.render( 'myEditor1' );  
  31. </script>  
  32. </body>  
  33. </html>  

更多推荐

HTML编辑器UEditor的简单使用

本文发布于:2023-03-25 07:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/d2a662f18fe7c951578fff6a99b28fae.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编辑器   简单   HTML   UEditor

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!