• 【富文本编辑器】Ueditor的demo——创建、修改——代码使用



    在这里插入图片描述

    富文本编辑器

    Ueditor的资源官网:http://fex.baidu.com/ueditor/
    资源下载官网:https://github.com/fex-team/ueditor

    简述:

    • 富文本编辑器,Multi-function Text Editor, 简称 MTE, 是一种可内嵌于浏览器,所见即所得的文本编辑器。
    • 富文本编辑器不同于文本编辑器,程序员可到网上下载免费的富文本编辑器内嵌于自己的网站或程序里(当然付费的功能会更强大些),方便用户编辑文章或信息。比较好的文本编辑器有kindeditor,fckeditor等。

    使用:

    对于支持富文本编辑的浏览器来说,其实就是设置 document 的 designMode 属性为 on 后,再通过执行 document.execCommand('commandName'[, UIFlag[, value]]) 即可。commandName 和 value 可以在MSDN 上和MDC 上找到,它们就是我们创建各种格式的命令,比方说我们要加粗字体,执行 document.execCommand('bold', false) 即可。

    1.下载的demo:

    官方资源在github上,不一定能进去:(个人资源下载不了的私信发)
    https://download.csdn.net/download/m0_70083523/87148892?spm=1001.2014.3001.5503

    官网进去有很多版本,我们使用的是jsp有关的:
    在这里插入图片描述

    压缩包内文件:index.html是完整demo文件
    在这里插入图片描述

    2.项目创建:

    eclipse创建web项目(idea原理相同,找到对应目录和jar包就好),在webapp目录下载创建一个文件夹(js),将文件夹中除了index.html都复制到新建的目录下。

    • 原本在压缩包文件jsp目录下的lib里面的jar包导入到WEB-INF的lib目录下
    • jsp目录下的config.json文件中有许多注释影响报红,将注释删除即可。
      在这里插入图片描述
    • 我将删除注释的纯净版config.json文件代码放下面了:
      {
          "imageActionName": "uploadimage",
          "imageFieldName": "upfile", 
          "imageMaxSize": 2048000, 
          "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"],
          "imageCompressEnable": true,
          "imageCompressBorder": 1600, 
          "imageInsertAlign": "none",
          "imageUrlPrefix": "/项目名",
          "imagePathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", 
                                    
          "scrawlActionName": "uploadscrawl", 
          "scrawlFieldName": "upfile", 
          "scrawlPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}",
          "scrawlMaxSize": 2048000,
          "scrawlUrlPrefix": "",
          "scrawlInsertAlign": "none",
      
          "snapscreenActionName": "uploadimage", 
          "snapscreenPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", 
          "snapscreenUrlPrefix": "",
          "snapscreenInsertAlign": "none", 
      
          "catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"],
          "catcherActionName": "catchimage", 
          "catcherFieldName": "source", 
          "catcherPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}",
          "catcherUrlPrefix": "", 
          "catcherMaxSize": 2048000, 
          "catcherAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"],
      
          "videoActionName": "uploadvideo", 
          "videoFieldName": "upfile", 
          "videoPathFormat": "/ueditor/jsp/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}",
          "videoUrlPrefix": "", 
          "videoMaxSize": 102400000,
          "videoAllowFiles": [
              ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
              ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"],
      
          "fileActionName": "uploadfile", 
          "fileFieldName": "upfile", 
          "filePathFormat": "/ueditor/jsp/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}", 
          "fileUrlPrefix": "", 
          "fileMaxSize": 51200000, 
          "fileAllowFiles": [
              ".png", ".jpg", ".jpeg", ".gif", ".bmp",
              ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
              ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
              ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
              ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
          ],
          
          "imageManagerActionName": "listimage", 
          "imageManagerListPath": "/ueditor/jsp/upload/image/", 
          "imageManagerListSize": 20, 
          "imageManagerUrlPrefix": "", 
          "imageManagerInsertAlign": "none", 
          "imageManagerAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], 
      
          "fileManagerActionName": "listfile", 
          "fileManagerListPath": "/ueditor/jsp/upload/file/", 
          "fileManagerUrlPrefix": "", 
          "fileManagerListSize": 20, 
          "fileManagerAllowFiles": [
              ".png", ".jpg", ".jpeg", ".gif", ".bmp",
              ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
              ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
              ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
              ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
          ] 
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68
      • 69
      • 70
      • 71
      • 72
      • 73
    3.修改代码:
    • 修改一:此文件中代码第10行 "imageUrlPrefix": " ",这里面要写入/项目名
      在这里插入图片描述
       "imageUrlPrefix": "/项目名",  /* 图片访问路径前缀 */
      
      • 1
    • 修改二:文件ueditor.config.js中添加代码:
      window.UEDITOR_HOME_URL = "/项目名/js/";  /*js是自己创建的那个文件,我们将配置等都放这里面*/
      
      • 1
      在这里插入图片描述
    • 此时富文本编辑器的所有配置文件已经与自己的项目绑定,创建jsp或者html页面引用编辑器。
    4.使用富文本编辑器:

    压缩包中index.html是完整的demo,提供了许多事件可以直接使用。如图:

    在这里插入图片描述


    在这里插入图片描述
    我们了解里面的方法,选择自己需要的。

    示例:

    webapp中创建index.jsp,也可以试试html页面:

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Insert title heretitle>
    head>
    <body>
    	/*绑定id*/
    	<script id="aaa" type="text/javascript">script>
    	/*选择自己要使用的事件*/
    	<button onclick="getContent()">获取内容button>
    body>
    	 /*这两个js很重要,src路径不能写错(/项目名/文件名/引用的js):引用编辑器*/   
        <script type="text/javascript" charset="utf-8" src="/Ueditor1_4_3-utf-8/js/ueditor.config.js">script>
        <script type="text/javascript" charset="utf-8" src="/Ueditor1_4_3-utf-8/js/ueditor.all.min.js"> script>
    
    <script type="text/javascript">
    	var editor = UE.getEditor('aaa');	
       /*  function getContent() {
            var arr = [];
            arr.push("使用editor.getContent()方法可以获得编辑器的内容");
            arr.push("内容为:");
            arr.push(UE.getEditor('editor').getContent());
            alert(arr.join("\n"));
        } */
    	function getContent() {
    		alert(editor.getContent());
    	}
    script>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    开启服务器(Tomcat),访问路径:http://localhost:8080/项目名/index.jsp
    在这里插入图片描述

  • 相关阅读:
    第二届全国高校计算机技能竞赛——C++赛道 题解
    在Linux中安装宝塔面板
    跨行业人群如何通过NPDP?过来人给的四点建议!
    Python数据分析--Numpy常用函数介绍(8)--Numpy中几中常见的图形
    快递物流类API推荐
    【云原生之Docker实战】使用Docker部署OrangeHRM人力资源管理系统
    鸿鹄工程项目管理系统em Spring Cloud+Spring Boot+前后端分离构建工程项目管理系统
    和运维工程师聊完,发现小丑竟是我自己
    axios在vue里简单的封装使用
    ESP8266-Arduino编程实例-PCF8591数据采集驱动
  • 原文地址:https://blog.csdn.net/m0_70083523/article/details/127835397