CKEditor - 删除DOM节点时销毁实例

Jos*_*ber 9 javascript ajax wysiwyg ckeditor

通过CKEditor文档阅读,我看到他们可以选择销毁实例CKEDITOR.instances.instanceName.destroy();.但是,如果DOM已更改,并且已删除整个WYSIWYG DOM结构,则Chrome中会出现以下错误:

Uncaught TypeError: Cannot read property 'document' of null
Run Code Online (Sandbox Code Playgroud)

...以及Firefox中的以下内容:

i.contentWindow is null
Run Code Online (Sandbox Code Playgroud)

有什么方法可以解决这个问题吗?

由于我的应用程序的结构方式(通过AJAX加载内容),.destroy()当元素仍在页面上时,我无法调用.

Eri*_*jak 16

如果你需要在AJAX调用之后销毁DOM中的ckeditor对象和elemnet,你可以通过在函数调用destroy(true)中设置一个布尔参数来实现.这样它就不会尝试更新DOM:

var editor = CKEDITOR.instances[name];
if (editor) { editor.destroy(true); }
CKEDITOR.replace(name);
Run Code Online (Sandbox Code Playgroud)

我已经编写了2个函数来更好地控制这些东西.请注意,我已经在使用这些函数之前声明了一个变量,但是有很多方法,但是这种方法对于我需要它的目的来说已经足够了(我使用并且只需要一个实例):

    if(typeof(editor) == 'undefined')
        var editor=null;

    function ck_delete(editor)
    {
        if(typeof(editor) != 'undefined' && editor!=null)
            editor.destroy();
    }

    function ck_init(ck_inst_name)
    {
        var el_id=document.getElementById(ck_inst_name);
        if(typeof(el_id) != 'undefined' && el_id!=null)
        {
            if(typeof(editor) == 'undefined' || editor==null)
            {
                editor=CKEDITOR.replace( ck_inst_name );
            }
            else
            {
                ck_delete(editor);
                editor=null;
                editor = CKEDITOR.replace( ck_inst_name );
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我还检查是否存在应该替换的HTML元素,因此我没有收到错误消息.