setData命令不是每次都将我的数据设置为CKEDITOR

Era*_*lea 2 javascript ckeditor

          CKEDITOR.replace('editor1', {
        contentsCss: '../assets/global/plugins/bootstrap/css/bootstrap.min.css',
        toolbar:
            [
                { name: 'document', groups: ['mode', 'document', 'doctools'], items: ['Source', '-', 'Save', 'NewPage', 'Preview', '-', 'Templates'] },
                { name: 'clipboard', groups: ['clipboard', 'undo'], items: ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'] },
                { name: 'editing', groups: ['find', 'selection', 'spellchecker'], items: ['Find', 'Replace', '-', 'SelectAll', '-', 'Scayt', 'ImageButton'] },
                { name: 'links', items: ['Link', 'Unlink', 'Anchor'] },
                '/',
                { name: 'basicstyles', groups: ['basicstyles', 'cleanup'], items: ['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat'] },
                { name: 'paragraph', groups: ['list', 'indent', 'blocks', 'align', 'bidi'], items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-', 'BidiLtr', 'BidiRtl', 'Language'] },
                '/',
                { name: 'styles', items: ['Styles', 'Format', 'Font', 'FontSize'] },
                { name: 'colors', items: ['TextColor', 'BGColor'] },
                { name: 'insert', items: ['Image', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak', 'Iframe', 'ShowBlocks'] }
            ]
    });



    MyAjaxMethod('GetDatabyId', IdParam, function (data) {
        var dt = data.d;
        if (dt!= null) {


            $("#foo").html(dt.asd);
            $("#foo2").val(dt.asdf);
            $("#foo3").val(dt.asdfg);

            CKEDITOR.instances.editor1.setData(dt.Detay);

        }
        else {
            alert('no data');
        }
    });
Run Code Online (Sandbox Code Playgroud)

它需要获取数据并设置一些formtools并最终设置CKEDITOR,但有时它不会将数据设置为CKEDITOR,有时也会.我尝试了一个简单的页面,使用相同的ajax方法和脚本,但它没有用,为什么会这样?我可以看到所有数据传入,json很好.我试图发送给CKEDITOR的html和inserthtml也没有用.我认为它试图在它取代CKEDITOR特征之前设置,但我无法做任何事情.

编辑:我该如何解决这个问题?(澄清我的具体问题)

EDIT2:由于ojovirtual托伦发现的问题,它被确认实例是设定数据前准备好.

ojo*_*ual 6

您应该setData仅在实例准备就绪时使用.它有时只能起作用,因为当你打电话时setData,CKEDITOR有时候没有完全初始化,有些则是.

有一个叫做instanceReady可以用来设置数据的事件:

CKEDITOR.on("instanceReady", function(event)
{
    CKEDITOR.instances.editor1.setData(dt.Detay);
});
Run Code Online (Sandbox Code Playgroud)

CKEDITOR文档