index.html 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <title>完整demo</title>
  6. <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  7. <script type="text/javascript" charset="utf-8" src="neditor.config.js"></script>
  8. <script type="text/javascript" charset="utf-8" src="neditor.all.min.js"> </script>
  9. <script type="text/javascript" charset="utf-8" src="neditor.service.js"></script>
  10. <!--建议手动加在语言,避免在ie下有时因为加载语言失败导致编辑器加载失败-->
  11. <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型,比如你在配置项目里配置的是英文,这里加载的中文,那最后就是中文-->
  12. <script type="text/javascript" charset="utf-8" src="i18n/zh-cn/zh-cn.js"></script>
  13. <script type="text/javascript" src="third-party/browser-md5-file.min.js"></script>
  14. <script type="text/javascript" src="third-party/jquery-1.10.2.min.js"></script>
  15. <style type="text/css">
  16. div{
  17. width:100%;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div>
  23. <h1>完整demo</h1>
  24. <script id="editor" type="text/plain" style="width:1024px;height:500px;"></script>
  25. </div>
  26. <div id="btns">
  27. <div>
  28. <button onclick="getAllHtml()">获得整个html的内容</button>
  29. <button onclick="getContent()">获得内容</button>
  30. <button onclick="setContent()">写入内容</button>
  31. <button onclick="setContent(true)">追加内容</button>
  32. <button onclick="getContentTxt()">获得纯文本</button>
  33. <button onclick="getPlainTxt()">获得带格式的纯文本</button>
  34. <button onclick="hasContent()">判断是否有内容</button>
  35. <button onclick="setFocus()">使编辑器获得焦点</button>
  36. <button onmousedown="isFocus(event)">编辑器是否获得焦点</button>
  37. <button onmousedown="setblur(event)" >编辑器失去焦点</button>
  38. </div>
  39. <div>
  40. <button onclick="getText()">获得当前选中的文本</button>
  41. <button onclick="insertHtml()">插入给定的内容</button>
  42. <button id="enable" onclick="setEnabled()">可以编辑</button>
  43. <button onclick="setDisabled()">不可编辑</button>
  44. <button onclick=" UE.getEditor('editor').setHide()">隐藏编辑器</button>
  45. <button onclick=" UE.getEditor('editor').setShow()">显示编辑器</button>
  46. <button onclick=" UE.getEditor('editor').setHeight(300)">设置高度为300默认关闭了自动长高</button>
  47. <button onclick=" UE.getEditor('editor').setPlaceholder('请输入内容')">设置placeholder</button>
  48. </div>
  49. <div>
  50. <button onclick="getLocalData()" >获取草稿箱内容</button>
  51. <button onclick="clearLocalData()" >清空草稿箱</button>
  52. </div>
  53. </div>
  54. <div>
  55. <button onclick="createEditor()">
  56. 创建编辑器</button>
  57. <button onclick="deleteEditor()">
  58. 删除编辑器</button>
  59. </div>
  60. <script type="text/javascript">
  61. //实例化编辑器
  62. //建议使用工厂方法getEditor创建和引用编辑器实例,如果在某个闭包下引用该编辑器,直接调用UE.getEditor('editor')就能拿到相关的实例
  63. var ue = UE.getEditor('editor');
  64. function isFocus(e){
  65. alert(UE.getEditor('editor').isFocus());
  66. UE.dom.domUtils.preventDefault(e)
  67. }
  68. function setblur(e){
  69. UE.getEditor('editor').blur();
  70. UE.dom.domUtils.preventDefault(e)
  71. }
  72. function insertHtml() {
  73. var value = prompt('插入html代码', '');
  74. UE.getEditor('editor').execCommand('insertHtml', value)
  75. }
  76. function createEditor() {
  77. enableBtn();
  78. UE.getEditor('editor');
  79. }
  80. function getAllHtml() {
  81. alert(UE.getEditor('editor').getAllHtml())
  82. }
  83. function getContent() {
  84. var arr = [];
  85. arr.push("使用editor.getContent()方法可以获得编辑器的内容");
  86. arr.push("内容为:");
  87. arr.push(UE.getEditor('editor').getContent());
  88. alert(arr.join("\n"));
  89. }
  90. function getPlainTxt() {
  91. var arr = [];
  92. arr.push("使用editor.getPlainTxt()方法可以获得编辑器的带格式的纯文本内容");
  93. arr.push("内容为:");
  94. arr.push(UE.getEditor('editor').getPlainTxt());
  95. alert(arr.join('\n'))
  96. }
  97. function setContent(isAppendTo) {
  98. var arr = [];
  99. arr.push("使用editor.setContent('欢迎使用ueditor')方法可以设置编辑器的内容");
  100. UE.getEditor('editor').setContent('欢迎使用ueditor', isAppendTo);
  101. alert(arr.join("\n"));
  102. }
  103. function setDisabled() {
  104. UE.getEditor('editor').setDisabled('fullscreen');
  105. disableBtn("enable");
  106. }
  107. function setEnabled() {
  108. UE.getEditor('editor').setEnabled();
  109. enableBtn();
  110. }
  111. function getText() {
  112. //当你点击按钮时编辑区域已经失去了焦点,如果直接用getText将不会得到内容,所以要在选回来,然后取得内容
  113. var range = UE.getEditor('editor').selection.getRange();
  114. range.select();
  115. var txt = UE.getEditor('editor').selection.getText();
  116. alert(txt)
  117. }
  118. function getContentTxt() {
  119. var arr = [];
  120. arr.push("使用editor.getContentTxt()方法可以获得编辑器的纯文本内容");
  121. arr.push("编辑器的纯文本内容为:");
  122. arr.push(UE.getEditor('editor').getContentTxt());
  123. alert(arr.join("\n"));
  124. }
  125. function hasContent() {
  126. var arr = [];
  127. arr.push("使用editor.hasContents()方法判断编辑器里是否有内容");
  128. arr.push("判断结果为:");
  129. arr.push(UE.getEditor('editor').hasContents());
  130. alert(arr.join("\n"));
  131. }
  132. function setFocus() {
  133. UE.getEditor('editor').focus();
  134. }
  135. function deleteEditor() {
  136. disableBtn();
  137. UE.getEditor('editor').destroy();
  138. }
  139. function disableBtn(str) {
  140. var div = document.getElementById('btns');
  141. var btns = UE.dom.domUtils.getElementsByTagName(div, "button");
  142. for (var i = 0, btn; btn = btns[i++];) {
  143. if (btn.id == str) {
  144. UE.dom.domUtils.removeAttributes(btn, ["disabled"]);
  145. } else {
  146. btn.setAttribute("disabled", "true");
  147. }
  148. }
  149. }
  150. function enableBtn() {
  151. var div = document.getElementById('btns');
  152. var btns = UE.dom.domUtils.getElementsByTagName(div, "button");
  153. for (var i = 0, btn; btn = btns[i++];) {
  154. UE.dom.domUtils.removeAttributes(btn, ["disabled"]);
  155. }
  156. }
  157. function getLocalData () {
  158. alert(UE.getEditor('editor').execCommand( "getlocaldata" ));
  159. }
  160. function clearLocalData () {
  161. UE.getEditor('editor').execCommand( "clearlocaldata" );
  162. alert("已清空草稿箱")
  163. }
  164. </script>
  165. </body>
  166. </html>