UEditor使用报错Cannot set property 'innerHTML' of undefined

仿用UEditor的setContent的时候报错,报错代码如下Uncaught TypeError: Cannot set property ‘innerHTML’ of undefined。调试ueditor.config.js,ueditor.all.js 提示me.body is undefined。

错误的原因是没有等UEditor创建完成就使用UEditor的setContent函数了,可以通过如下两种代码解决 :

1.

ueditor.addListener("ready", function () { 
    ueditor.setContent('str'); 
});

2.

ueditor.ready(function() { 
    ueditor.setContent('str'); 
});

ps:

var ue = UE.getEditor('containerId');
    var a  = "<%= info.C_Introdution || '' %>";
    a = escape2Html(a);
    $("#introdution").html(a);

    function escape2Html(str) {
        if(str.indexOf("&#34;")){
            str = str.replace(/&#34;/ig,"&quot;");
        }
        var arrEntities={'&#34;':'"','lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'};
        return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];});
    }

    ue.ready(function() {
        ue.setContent(a);
        ue.addListener("contentchange", function () {
            $("#introdution").html(ue.getContent());
        })
    });

转自:https://blog.csdn.net/chengshaoying/article/details/51841515

猜你喜欢

转载自blog.csdn.net/qq_29207823/article/details/82020617