首页 > 其他分享 >wangEditor的使用

wangEditor的使用

时间:2023-03-27 16:24:12浏览次数:35  
标签:插入 wangEditor xhr result 使用 editor 上传 图片

第一步,将其下载,并引入项目中。

image

第二步,引入js

<script type="text/javascript" src="/plugin/wangEditor/release/wangEditor.min.js"></script>

第三步,初始化对象

 <span class="col-lg-8" id="editor"></span>
var E = window.wangEditor;
var editor = new E('#editor');
editor.create();

第四步,添加一些配置

配置菜单

// 自定义菜单配置
editor.customConfig.menus = [
    'head',  // 标题
    'bold',  // 粗体
    'fontSize',  // 字号
    'fontName',  // 字体
    'italic',  // 斜体
    'underline',  // 下划线
    'strikeThrough',  // 删除线
    'foreColor',  // 文字颜色
    'backColor',  // 背景颜色
    'link',  // 插入链接
    'list',  // 列表
    'justify',  // 对齐方式
    'quote',  // 引用
    'emoticon',  // 表情
    'image',  // 插入图片
    'table',  // 表格
    'video',  // 插入视频
    'code',  // 插入代码
    'undo',  // 撤销
    'redo'  // 重复
];

配置图片上传


editor.customConfig.uploadImgServer = '/admin.php/Upload/wang_editor';  // 上传图片到服务器
// 3M
editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024;
// 限制一次最多上传 5 张图片
editor.customConfig.uploadImgMaxLength = 1;
// 自定义文件名
editor.customConfig.uploadFileName = 'editor_img';
// 将 timeout 时间改为 3s
editor.customConfig.uploadImgTimeout = 5000;

editor.customConfig.uploadImgHooks = {
    before: function (xhr, editor, files) {
        // 图片上传之前触发
        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,files 是选择的图片文件

        // 如果返回的结果是 {prevent: true, msg: 'xxxx'} 则表示用户放弃上传
        // return {
        //     prevent: true,
        //     msg: '放弃上传'
        // }
        // alert("前奏");
    },
    success: function (xhr, editor, result) {
        // 图片上传并返回结果,图片插入成功之后触发
        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
        // var url = result.data.url;
        // alert(JSON.stringify(url));
        // editor.txt.append(url);
        // alert("成功");
    },
    fail: function (xhr, editor, result) {
        // 图片上传并返回结果,但图片插入错误时触发
        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
        alert("失败");
    },
    error: function (xhr, editor) {
        // 图片上传出错时触发
        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象
        // alert("错误");
    },
    // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式,可使用该配置
    // (但是,服务器端返回的必须是一个 JSON 格式字符串!!!否则会报错)
    customInsert: function (insertImg, result, editor) {
        // 图片上传并返回结果,自定义插入图片的事件(而不是编辑器自动插入图片!!!)
        // insertImg 是插入图片的函数,editor 是编辑器对象,result 是服务器端返回的结果
        // 举例:假如上传图片成功后,服务器端返回的是 {url:'....'} 这种格式,即可这样插入图片:
        var url = result.data[0];
        insertImg(url);
        // result 必须是一个 JSON 格式字符串!!!否则报错
    }
}

第五步,设置内容

// 设置内容
editor.txt.html(content);

第六步,获取内容

// 获取内容
var content = editor.txt.html();

来源:https://www.cnblogs.com/jiqing9006/p/9156957.html

标签:插入,wangEditor,xhr,result,使用,editor,上传,图片
From: https://www.cnblogs.com/hefeng2014/p/17261932.html

相关文章

  • 优化 vue 使用 webpack 打包,出现的缓存问题 及 项目部署问题
    https://blog.csdn.net/qq_39264561/article/details/124844878`constpath=require("path");constwebpack=require("webpack");consttimeStamp=newDate().ge......
  • 是时候选择一款富文本编辑器了(wangEditor)
    需要一款富文本编辑器,当然不能自己造轮子。本来想使用cnblog也在用的TinyMCE,名气大,功能全。但是发现TinyMCE从4.0开始,不再支持直接下载。所以还是决定选用wangEdit......
  • C# 使用 FreeSpire.XLS 将Excel转PDF
    Spire是收费的插件,他有一个免费的版(FreeSpire.XLS),我们下面就安装FreeSpire.XLS来进行操作。1、安装 FreeSpire.XLSInstall-PackageFreeSpire.XLS2、引用Spire.......
  • 使用SLF4J和Logback23
    其实SLF4J类似于CommonsLogging,也是一个日志接口,而Logback类似于Log4j,是一个日志的实现。为什么有了CommonsLogging和Log4j,又会蹦出来SLF4J和Logback?这是因为Java有着非常......
  • Git使用 | 解决github下载源码慢的问题
    最近,准备在github上克隆一些开源代码来学习。通常情形就是,网页半天打不开或者无法访问,大一点的git仓库也无法克隆下来。后续,通过gitee码云加速下载github代码,解决了问题。下......
  • pve proxmox virtual environment 系统安装 使用 qcow2
    在pve系统下载qcow2镜像centos7:https://cloud.centos.org/centos/7/images/wgethttps://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2在p......
  • tableau——安装,学习和简单使用
    1.安装       2.学习可视化原理  小原理:视觉映射    大原理: 3.简单使用 ......
  • PsTool使用手册
    簡介WindowsNT和Windows2000資源套件隨附許多命令列工具,可協助您管理WindowsNT/2K系統。經過一段時間後,我已經成長了一組類似的工具,包括資源套件中未包含的部分......
  • 直播系统搭建,Android使用RadioGroup+RadioButton实现导航栏
    直播系统搭建,Android使用RadioGroup+RadioButton实现导航栏主页面代码packagecom.example.taobaounion.ui.activity;importandroid.app.Activity;importandroid.os.Bu......
  • Linux下JDK和JMeter部署及使用
    一、在Linux下安装JDK并配置环境变量1、下载JDKJDK8下载地址:https://www.oracle.com/java/technologies/javase/javase8-archive-downloads.html2、rz命令上传到服务器......