CKEditor实例已经存在。

99

我正在使用jQuery对话框呈现通过AJAX获取的表单。在某些表单中,我使用CKEditor来处理文本区域。编辑器在第一次加载时显示得很好。

当用户取消对话框时,我会删除内容,以便它们在稍后的请求中重新加载。问题是,一旦对话框重新加载,CKEditor就会声称编辑器已经存在。

uncaught exception: [CKEDITOR.editor] The instance "textarea_name" already exists.

这个API包含一个销毁现有编辑器的方法,我看到有些人声称这是一个解决方案:

if (CKEDITOR.instances['textarea_name']) {
CKEDITOR.instances['textarea_name'].destroy();
}
CKEDITOR.replace('textarea_name');

这对我来说不起作用,因为我收到了一个新的错误:

TypeError: Result of expression 'i.contentWindow' [null] is not an object.

这个错误似乎发生在“destroy()”而不是“replace()”。是否有人遇到过这种情况并找到了不同的解决方法?

是否可以“重新渲染”现有的编辑器,而不是销毁和替换它?

更新:这里还有一个问题涉及相同的问题,但他提供了可下载的测试案例

32个回答

-1
如果您知道编辑器的名称,那么这就很简单了。
例如,如果编辑器的名称是editor1(这是div或textarea的id),那么您只需像这样检查即可。
if(CKEDITOR.instances['editor1']){
   // Instance present
}else{
   // Instance not present 
} 

如果你想获取已初始化的编辑器数量,可以像下面这样做:
for(let inst of CKEDITOR.instances){
   // inst is an Obj of editor instance
   // inst.name will give the name of the editor instance
}

-1

你不需要销毁CKeditor对象,而是需要使用remove()方法进行移除:

将以下代码进行修改:

CKEDITOR.instances['textarea_name'].destroy();

为此:

CKEDITOR.remove(CKEDITOR.instances['textarea_name']);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接