tinymce编辑器+layui

tinymce功能很强大的一款编辑器

源码以及参考资料https://gitee.com/chick1993/layui-tinymce

代码如下 html:

                        <div >
                        	<textarea id="edit"></textarea>
                        </div>

Js(上传图片注意修改为自己的地址)

var content = "";
var edit = "";    
layui.extend({
        tinymce: '{/}./存放路径'
    }).use(['tinymce'], function () {
    	var t = layui.tinymce;
        t.render({
            elem: "#edit",
            height: 600,
            //images_upload_url:"/自己的上传地址"//二进制
            //重写上传方法(带进度条),不然传的二进制,以下返回的结果格式:{ location : "/demo/image/1.jpg" }
            images_upload_handler: function (blobInfo, success, failure, progress) {
                    var xhr, formData;
                    xhr = new XMLHttpRequest();
                    xhr.withCredentials = false;
                    xhr.open('POST', '/自己的上传地址');

                    xhr.upload.onprogress = function(e){
                        progress(e.loaded / e.total * 100);
                    }

                    xhr.onload = function() {
                        var json;
                        if (xhr.status == 403) {
                            failure('HTTP Error: ' + xhr.status, { remove: true });
                            return;
                        }
                        if (xhr.status < 200 || xhr.status >= 300 ) {
                            failure('HTTP Error: ' + xhr.status);
                            return;
                        }
                        json = JSON.parse(xhr.responseText);
                        if (!json || typeof json.location != 'string') {
                            failure('Invalid JSON: ' + xhr.responseText);
                            return;
                        }
                        success(json.location);
                    };

                    xhr.onerror = function () {
                        failure('Image upload failed due to a XHR Transport error. Code: ' + xhr.status);
                    }

                    formData = new FormData();
                    formData.append('file', blobInfo.blob(), blobInfo.filename());
                    
                    xhr.send(formData);
                }
            	
            	
        },(opt, d)=>{
            // 加载完成后回调 opt 是传入的所有参数
            // edit是当前编辑器实例,等同于t.get返回值
        	edit = d;
        	d.setContent(content);//回显
        	
        });
       
    })

上传注意返回结果格式:

{ location : "/demo/image/1.jpg" }

取值

var ftext = edit.getContent();//获得富文本内容

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值