usage.rst 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 编辑器使用方法
  2. ========================================================
  3. 1. 下载编辑器
  4. -----------------------------------------------------------------
  5. 下载 KindEditor 最新版本,下载之后打开 examples/index.html 就可以看到演示。
  6. 下载页面: http://www.kindsoft.net/down.php
  7. 2. 部署编辑器
  8. -----------------------------------------------------------------
  9. 解压 kindeditor-x.x.x.zip 文件,将所有文件上传到您的网站程序目录里,例如:http://您的域名/editor/
  10. .. note::
  11. 您可以根据需求删除以下目录后上传到服务器。
  12. * asp - ASP程序
  13. * asp.net - ASP.NET程序
  14. * php - PHP程序
  15. * jsp - JSP程序
  16. * examples - 演示文件
  17. 3. 修改HTML页面
  18. -----------------------------------------------------------------
  19. 1) 在需要显示编辑器的位置添加textarea输入框。
  20. .. sourcecode:: html
  21. <textarea id="editor_id" name="content" style="width:700px;height:300px;">
  22. &lt;strong&gt;HTML内容&lt;/strong&gt;
  23. </textarea>
  24. .. note::
  25. * id在当前页面必须是唯一的值。
  26. * 在textarea里设置HTML内容即可实现编辑,在这里需要注意的是,如果从服务器端程序(ASP、PHP、ASP.NET等)直接显示内容,则必须转换HTML特殊字符(>,<,&,")。具体请参考各语言目录下面的demo.xxx程序,目前支持ASP、ASP.NET、PHP、JSP。
  27. * 在有些浏览器上不设宽度和高度可能显示有问题,所以最好设一下宽度和高度。宽度和高度可用inline样式设置,也可用 :doc:`option` 设置。
  28. 2) 在该HTML页面添加以下脚本。
  29. .. sourcecode:: html
  30. <script charset="utf-8" src="/editor/kindeditor.js"></script>
  31. <script charset="utf-8" src="/editor/lang/zh-CN.js"></script>
  32. <script>
  33. KindEditor.ready(function(K) {
  34. window.editor = K.create('#editor_id');
  35. });
  36. </script>
  37. .. note ::
  38. * 第一个参数可用其它CSS选择器,匹配多个textarea时只在第一个元素上加载编辑器。
  39. * 通过K.create函数的第二个参数,可以对编辑器进行配置,具体参数请参考 :doc:`option` 。
  40. .. sourcecode:: js
  41. var options = {
  42. cssPath : '/css/index.css',
  43. filterMode : true
  44. };
  45. var editor = K.create('textarea[name="content"]', options);
  46. 4. 获取HTML数据
  47. -----------------------------------------------------------------
  48. .. sourcecode:: js
  49. // 取得HTML内容
  50. html = editor.html();
  51. // 同步数据后可以直接取得textarea的value
  52. editor.sync();
  53. html = document.getElementById('editor_id').value; // 原生API
  54. html = K('#editor_id').val(); // KindEditor Node API
  55. html = $('#editor_id').val(); // jQuery
  56. // 设置HTML内容
  57. editor.html('HTML内容');
  58. .. note ::
  59. * KindEditor的可视化操作在新创建的iframe上执行,代码模式下的textarea框也是新创建的,所以最后提交前需要执行 :ref:`KEditor.sync` 将HTML数据设置到原来的textarea。
  60. * KindEditor在默认情况下自动寻找textarea所属的form元素,找到form后onsubmit事件里添加sync函数,所以用form方式提交数据,不需要手动执行sync()函数。
  61. * KindEditor默认采用白名单过滤方式,可用 :ref:`htmlTags` 参数定义要保留的标签和属性。当然也可以用 :ref:`filterMode` 参数关闭过滤模式,保留所有标签。
  62. .. sourcecode:: js
  63. // 关闭过滤模式,保留所有标签
  64. KindEditor.options.filterMode = false;
  65. KindEditor.ready(function(K)) {
  66. K.create('#editor_id');
  67. }