index.html 6.3 KB

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