Javascript 如何正确销毁CKEditor实例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3613215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:26:15  来源:igfitidea点击:

How to properly destroy CKEditor instance?

javascriptckeditor

提问by Shane Reustle

I am running CKeditor 3.4 on a pretty simple page. I am having a problem (sometimes) where when I call document.main_form.submit(), it will not send along the contents of the textarea. After some reading, it sounds like CKeditor is not destroying properly. I tried to manually destroy it before I save the form, but wasn't able to call it. The weird thing is, it works sometimes, but not others. I'm on Chrome, so that may be screwing with things, but the same thing happens in Firefox.

我在一个非常简单的页面上运行 CKeditor 3.4。我有一个问题(有时),当我调用 document.main_form.submit() 时,它不会沿着 textarea 的内容发送。经过一些阅读,听起来 CKeditor 没有正确销毁。我试图在保存表单之前手动销毁它,但无法调用它。奇怪的是,它有时有效,但有时无效。我在 Chrome 上,所以这可能会搞砸,但同样的事情发生在 Firefox 中。

How can I properly destroy the CKeditor so that it always sends the textarea data in POST. Thanks!

如何正确销毁 CKeditor 以便它始终在 POST 中发送 textarea 数据。谢谢!

回答by Laramie

I had this problem. What a pain.

我有这个问题。多么痛苦。

To properly destroy the editor instance, try

要正确销毁编辑器实例,请尝试

if (CKEDITOR.instances.myInstanceName) CKEDITOR.instances.myInstanceName.destroy();

From the documentation here

这里的文档

I solved the missing content issue by assigning the contents of the editor to a hidden field prior to postback. I'm using ASP.Net, but it should work universally.

我通过在回发之前将编辑器的内容分配给隐藏字段来解决丢失的内容问题。我正在使用 ASP.Net,但它应该可以通用。

in the client-side click handler of the submit button, call

在提交按钮的客户端单击处理程序中,调用

if (CKEDITOR.instances.myInstanceName)
    document.getElementById('hiddenField').value = CKEDITOR.instances.getData();

回答by Bernie GGGG

I once used angularjs ui-router with one CKEDITOR instance for each sub-view. I used the following solution to clear the instances every time I load the ui-view

我曾经将 angularjs ui-router 与每个子视图的一个 CKEDITOR 实例一起使用。每次加载 ui-view 时,我都使用以下解决方案清除实例

for(name in CKEDITOR.instances)
{
    CKEDITOR.instances[name].destroy()
}

回答by Tomasz Dzi?cielewski

In my situation

在我的情况下

CKEDITOR.instances.myInstanceName.destroy();

didn't help, because I'd opened CKEditor in jquery dialogon double click on some item. When I closed editor and then opened them again, my code crashed.
Solution was using

没有帮助,因为我jquery dialog在某些项目上双击打开了 CKEditor 。当我关闭编辑器然后再次打开它们时,我的代码崩溃了。
解决方案是使用

CKEDITOR.instances.myInstanceName.destroy(false);

which updated DOM element (link to documentation).

其中更新了 DOM 元素(链接到文档)。

回答by sandeep kumar

use this simple code.Note my text area id is editor1.
or You can also check it with console.log(CKEDITOR.instances.editor1);

使用这个简单的代码。注意我的文本区域 ID 是editor1
或者你也可以用console.log(CKEDITOR.instances.editor1);

if (CKEDITOR.instances.editor1) {
     CKEDITOR.instances.editor1.destroy();
}

回答by ashish naghate

for(name in CKEDITOR.instances){ CKEDITOR.instances[name].destroy() }

for(name in CKEDITOR.instances){ CKEDITOR.instances[name].destroy() }

Use to code to destroy all the instances created by ckeditor

使用to code销毁ckeditor创建的所有实例

回答by Will Berger

the solution that finally did work.

最终奏效的解决方案。

The issue was if you destroy a ckeditor and the subsequently try to replace a textarea that does not work.

问题是,如果您销毁 ckeditor,然后尝试替换不起作用的 textarea。

I found this simple example that gave me the clue. Use div and append the ckeditor instead of using replace api call

我发现这个简单的例子给了我线索。使用 div 并附加 ckeditor 而不是使用替换 api 调用

http://ckeditor.com/latest/samples/old/ajax.html

http://ckeditor.com/latest/samples/old/ajax.html

<div id='emailEditor1'>
              </div>

function closeTab() {
    emailEditor1.destroy();
    emailEditor1 = null;
}

function createEditor() 
    if (emailEditor1 == null) {
        emailEditor1 = CKEDITOR.appendTo( 'emailEditor1');
    }
}

回答by shobhit pokhriyal

$this->widget('cms.extensions.fancybox.EFancyBox', array(
    'target' => 'a#fancy-link',
    'config' => array( 'onClosed'=>'js:function(){for(name in CKEDITOR.instances){ CKEDITOR.instances[name].destroy(true);}}'
)));