mcya / webTechnology

🙃 A practical web technology website(web前端开发的实用网站收集,这些是关于真正技术或是提高开发效率以及相关书签笔记)
5 stars 4 forks source link

KindEditor 上传回调 #2

Open mcya opened 7 years ago

mcya commented 7 years ago

上传回调

KindEditor.ready(function (K) {  
    var editor = K.create('#editor_id', {  
        //上传管理  
        uploadJson: '/js/kindeditor/asp.net/upload_json.ashx',  
        //文件管理  
        fileManagerJson: '/js/kindeditor/asp.net/file_manager_json.ashx',  
        allowFileManager: true,  
        //设置编辑器创建后执行的回调函数  
        afterCreate: function () {  
            var self = this;  
            K.ctrl(document, 13, function () {  
                self.sync();  
                K('form[name=example]')[0].submit();  
            });  
            K.ctrl(self.edit.doc, 13, function () {  
                self.sync();  
                K('form[name=example]')[0].submit();  
            });  
        },  
        //上传文件后执行的回调函数,获取上传图片的路径  
        afterUpload: function (url) {  
            var a = $("#uppics").val();  
            if (a.length > 0) {  
                $("#uppics").val(a + "," + url);  
            } else {  
                $("#uppics").val(url);  
            }  
            //alert($("#uppics").val())  
        },  
        afterCreate: function () {  //要取值设置这里 这个函数就是同步KindEditor的值到textarea文本框  
            this.sync();  
        },  
        afterBlur: function () {  //同时设置这里   
            this.sync();  
        },  
        items:  
                [  
                        'justifyleft', 'justifycenter', 'justifyright', 'justifyfull',  
                        'emoticons', 'multiimage', 'baidumap', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',  
                        'superscript', 'formatblock', 'fontname', 'fontsize', 'forecolor', 'hilitecolor', 'bold',  
                        'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat'  
                ]  
    });  
});  
mcya commented 7 years ago

编辑HTML的内容之(一):

替换src属性值

正则表示总是跟不上节奏,就只能这么处理了---分割img,循环替换。

var dataSource = {}
    dataSource.values = editor.html()
    var chuliData = "<div>"+String(dataSource.values)+"</div>"
    var imgArr = chuliData.split("<img")
    for (var i = imgArr.length - 1; i >= 1; i--) {
        chuliData = chuliData.replace('/eac-core/res/kindeditor/handler/showImage.jsp?path', '/page/getImgForNews.jsp?photoUrl');
    }