index.html 5.7 KB

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