CKEditor 实例已存在
我正在使用 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()”上,而不是发生在“destroy()”上“替换()”。有没有人经历过这个并找到不同的解决方案?
是否可以“重新渲染”现有编辑器,而不是销毁和替换它?
已更新 这是处理相同问题的另一个问题,但他提供了可下载测试用例。
I am using jquery dialogs to present forms (fetched via AJAX). On some forms I am using a CKEditor for the textareas. The editor displays fine on the first load.
When the user cancels the dialog, I am removing the contents so that they are loaded fresh on a later request. The issue is, once the dialog is reloaded, the CKEditor claims the editor already exists.
uncaught exception: [CKEDITOR.editor] The instance "textarea_name" already exists.
The API includes a method for destroying existing editors, and I have seen people claiming this is a solution:
if (CKEDITOR.instances['textarea_name']) {
CKEDITOR.instances['textarea_name'].destroy();
}
CKEDITOR.replace('textarea_name');
This is not working for me, as I receive a new error instead:
TypeError: Result of expression 'i.contentWindow' [null] is not an object.
This error seems to occur on the "destroy()" rather than the "replace()". Has anyone experienced this and found a different solution?
Is is possible to 're-render' the existing editor, rather than destroying and replacing it?
UPDATED
Here is another question dealing with the same problem, but he has provided a downloadable test case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
为此,您需要在销毁实例时传递布尔参数true:
For this to work you need to pass boolean parameter true when destroying instance:
我也遇到了这个问题,但我用一种更简单的方式解决了它......
我在 jQuery 脚本中使用类“ckeditor”作为我想要用于 CKEditor 的文本区域的选择器。默认的 ckeditor JS 脚本也使用此类来标识 CKEditor 使用哪些文本区域。
这意味着我的 jQuery 脚本和默认的 ckeditor 脚本之间存在冲突。
我只是将 textarea 的类和我的 jQuery 脚本更改为“do_ckeditor”(您可以使用除“ckeditor”之外的任何内容)并且它起作用了。
I had this problem too, but I solved it in a much simpler way...
I was using the class "ckeditor" in my jQuery script as the selector for which textareas I wanted use for CKEditor. The default ckeditor JS script also uses this class to identify which textareas to use for CKEditor.
This meant there is a conflict between my jQuery script and the default ckeditor script.
I simply changed the class of the textarea and my jQuery script to 'do_ckeditor'(you could use anything except "ckeditor") and it worked.
这是对我有用的最简单(也是唯一)的解决方案:
删除数组中的此项可以防止此表单安全检查破坏您的应用程序。
destroy() 和 remove() 对我不起作用。
This is the simplest (and only) solution that worked for me:
Deleting this entry in the array prevents this form safety check from destroying your application.
destroy() and remove() did not work for me.
也许这会对你有所帮助 - 我已经使用 jquery 做了类似的事情,除了我加载了未知数量的 ckeditor 对象。我花了一段时间才偶然发现这一点 - 文档中并不清楚。
以下是我从编辑处获取内容所运行的内容:
更新:我已更改我的答案以使用正确的方法 - 即 .destroy()。 .remove() 是内部的,并且一度没有正确记录。
Perhaps this will help you out - I've done something similar using jquery, except I'm loading up an unknown number of ckeditor objects. It took my a while to stumble onto this - it's not clear in the documentation.
And here is what I run to get the content from the editors:
UPDATE: I've changed my answer to use the correct method - which is .destroy(). .remove() is meant to be internal, and was improperly documented at one point.
我遇到过类似的问题,我们为通过 ajax 加载的内容创建了多个 CKeditor 实例。
将 DOM 保留在内存中,并且没有删除所有绑定。
每当我使用来自 ajax 的新数据创建新实例时,都会出现错误 i.contentWindow 错误。但这只是直到我发现我在清除 DOM 后销毁了实例。
在实例和实例时使用destroy()它的 DOM 存在于页面上,那么它就可以正常工作。
I've had similar issue where we were making several instances of CKeditor for the content loaded via ajax.
Kept the DOM in the memory and didn't remove all the bindings.
Gave the error i.contentWindow error whenever I create new instance with new data from ajax. But this was only until I figured out that I was destroying the instance after clearing the DOM.
Use destroy() while the instance & it's DOM is present on the page, then it works perfectly fine.
对于 ajax 请求,
此片段会从文档中删除所有实例。
然后创建新实例。
For ajax requests,
this snipped removes all instances from document.
Then creates new instances.
在绑定到不再位于 DOM 中的文本区域的编辑器实例上调用 destroy 时,似乎会发生
i.contentWindow is null
错误。CKEDITORY.destroy
采用参数noUpdate
。API 文档指出:
因此,为了避免该错误,请在从 DOM 中删除 textarea 元素之前调用 destroy,或者调用 destory(true) 以避免尝试更新不存在的 DOM 元素。
(使用版本 3.6.2 和 jQuery 适配器)
The
i.contentWindow is null
error seems to occur when calling destroy on an editor instance that was tied to a textarea no longer in the DOM.CKEDITORY.destroy
takes a parameternoUpdate
.The APIdoc states:
So, to avoid the error, either call destroy before removing the textarea element from the DOM, or call destory(true) to avoid trying to update the non-existent DOM element.
(using version 3.6.2 with jQuery adapter)
这对我有用:
This is what worked for me:
我在调用创建实例(每个页面加载一个实例)之前使用它。不确定这如何影响内存处理以及不影响什么。仅当您想要替换页面上的所有实例时,这才有效。
I am using this before my calls to create an instance (ones per page load). Not sure how this affects memory handling and what not. This would only work if you wanted to replace all of the instances on a page.
我已经根据上述所有代码准备了自己的解决方案。
它非常适合我。
有时 AJAX 请求后会出现错误的 DOM 结构。
例如:
这也会导致问题,并且 ckEditor 将无法工作。因此请确保您拥有正确的 DOM 结构。
I've prepared my own solution based on all above codes.
It works perfectly for me.
Sometimes after AJAX request there is wrong DOM structure.
For instace:
This will cause issue as well, and ckEditor will not work. So make sure that you have correct DOM structure.
我在实例上遇到了同样的问题,我到处寻找,最后这个实现对我有用:
i had the same problem with instances, i was looking everywhere and finally this implementation works for me:
您可以通过ckeditor的remove方法删除任何ckeditor实例。实例将是文本区域的 ID 或名称。
You can remove any ckeditor instance by remove method of ckeditor. Instance will be id or name of the textarea.
这个函数在 CKEditor 版本 4.4.5 中适用于我,它没有任何内存泄漏
// 调用这个函数如下
This functions works for me in CKEditor version 4.4.5, it does not have any memory leaks
// call this function as below
事实上,从代码中删除“.ckeditor”类可以解决该问题。我们大多数人都遵循 ckeditor 文档中的 jQuery 集成示例:
并认为“...也许我可以去掉 '.jquery_' 部分”。
我一直在浪费时间调整回调函数(因为 {skin:'office2003'} 实际上有效),而问题却来自其他地方。
我认为文档应该提到不建议使用“ckeditor”作为类名,因为它是保留关键字。
干杯。
Indeed, removing the ".ckeditor" class from your code solves the issue. Most of us followed the jQuery integration example from the ckeditor's documentation:
and thought "... maybe I can just get rid or the '.jquery_' part".
I've been wasting my time tweaking the callback function (because the {skin:'office2003'} actually worked), while the problem was coming from elsewhere.
I think the documentation should mention that the use of "ckeditor" as a class name is not recommended, because it is a reserved keyword.
Cheers.
我了解到
删除 CKEDITOR.instances[editorName];
其本身实际上删除了该实例。我读过和看到的所有其他方法,包括在 stackoverflow 上从其用户那里找到的方法,对我来说都不起作用。
在我的情况下,我使用 ajax 调用来提取围绕 和 的内容的副本。问题恰好是因为我使用 jQuery .live 事件绑定“编辑此文档”链接,然后在 ajax 加载成功后应用 ckeditor 实例。这意味着,当我单击另一个链接与另一个 .live 事件的链接时,我必须使用删除 CKEDITOR.instances[editorName] 作为清除内容窗口(保存表单)的任务的一部分,然后重新获取保存的内容在数据库或其他资源中。
I learned that
delete CKEDITOR.instances[editorName];
by itself, actually removed the instance. ALL other methods i have read and seen, including what was found here at stackoverflow from its users, did not work for me.
In my situation, im using an ajax call to pull a copy of the content wrapped around the and 's. The problem happens to be because i am using a jQuery .live event to bind a "Edit this document" link and then applying the ckeditor instance after success of the ajax load. This means, that when i click another link a link with another .live event, i must use the delete CKEDITOR.instances[editorName] as part of my task of clearing the content window (holding the form), then re-fetching content held in the database or other resource.
我在 jQuery 对话框中也遇到了同样的问题。
如果您只想删除以前的数据,为什么要销毁实例?
I hade the same problem with a jQuery Dialog.
Why destroy the instance if you just want to remove previous data ?
我选择重命名所有实例而不是销毁/替换 - 因为有时 AJAX 加载的实例并没有真正替换页面核心上的实例...在 RAM 中保留更多实例,但这样可以减少冲突。
I chose to rename all instances instead of destroy/replace - since sometimes the AJAX loaded instance doesn't really replace the one on the core of the page... keeps more in RAM, but less conflict this way.
我所处的情况是,我必须控制生成对话框,每个对话框都需要在这些对话框中嵌入一个 ckeditor。碰巧文本区域共享相同的 id。 (通常这是非常糟糕的做法,但我有 2 个 jqGrid,一个已分配的项目,另一个未分配的项目。)它们共享几乎相同的配置。因此,我使用通用代码来配置两者。
因此,当我加载对话框时,为了添加行或编辑它们,可以从 jqGrid;我必须删除所有文本区域中 CKEDITOR 的所有实例。
这将循环遍历所有文本区域,如果存在 CKEDITOR 实例,则销毁它。
或者,如果您使用纯 jQuery:
I am in the situation where I have to controls that spawn dialogs, each of them need to have a ckeditor embedded inside these dialogs. And it just so happens the text areas share the same id. (normally this is very bad practice, but I have 2 jqGrids, one of assigned items and another of unassigned items.) They share almost identical configuration. Thus, I am using common code to configure both.
So, when I load a dialog, for adding rows, or for editing them, from either jqGrid; I must remove all instances of CKEDITOR in all textareas.
This will loop over all textareas, and if there is a CKEDITOR instance, then destroy it.
Alternatively if you use pure jQuery:
删除
class="ckeditor"
,它可能触发了 ckeditor 初始化remove
class="ckeditor"
, it might have triggered ckeditor initialization我遇到了同样的问题,收到空引用异常,并且编辑器中将显示“null”一词。我尝试了一些解决方案,包括将编辑器升级到3.4.1,但均无济于事。
我最终不得不编辑源代码。在 _source\plugins\wysiwygarea\plugin.js 中的大约第 416 到 426 行,有这样的片段:
至少在 FF 中,iframe 在需要时还没有完全实例化。我用 setTimeout 函数包围了该行之后的函数的其余部分:
文本现在在模式对话框中一致呈现。
I had the same problem where I was receiving a null reference exception and the word "null" would be displayed in the editor. I tried a handful of solutions, including upgrading the editor to 3.4.1 to no avail.
I ended up having to edit the source. At about line 416 to 426 in _source\plugins\wysiwygarea\plugin.js, there's a snippet like this:
In FF at least, the iframe isn't completely instantiated by the time it's needed. I surrounded the rest of the function after that line with a setTimeout function:
The text renders consistently now in the modal dialogs.
为了支持动态(Ajax)加载表单(之间不刷新页面),其中包含具有相同(再次调用相同表单)或不同 ID(先前卸载的表单)的文本区域,并将它们转换为 CKEditor 元素,我执行了以下操作(使用 JQuery适配器):
页面完成每次传递要转换的文本区域的 Ajax 调用后,我会调用以下函数:
看起来像这样(它假设要转换为 RTE 的文本区域具有 class="yourCKClass “):
我应该提到这一行:
可以(并且应该)很简单:
但是我发现编辑器通常会在加载后显示正确的内容一秒钟,然后清空编辑器中所需的内容。因此,带有回调代码的行强制 CKEditor 内容与原始文本区域内容相同。使用时会引起闪烁。如果您可以避免使用它,请这样做..
To support dynamic (Ajax) loading of forms (without page refreshes between) which contain textareas with the same (same form is called again) or different ID's (previously unloaded form) and convert them to CKEditor elements I did the following (using the JQuery adapter):
After the page has finished every Ajax call that delivers a textarea to be converted, I make a call to the following function:
This looks like this (it assumes your textareas to be converted to RTE's have class="yourCKClass"):
I should mention that the line:
could (and should) be simply:
however I found that the editor would often show the correct content for a second after loading and them empty the editor of the desired content. So that line with the callback code forces the CKEditor content to be the same as the originating textarea content. Causes a flicker when used. If you can avoid using it, do so..
我和杰克博伯格有完全相同的问题。我使用动态表单加载到 jquery 对话框中,然后附加各种小部件(日期选择器、ckeditors 等...)。
我尝试了上面提到的所有解决方案,但没有一个对我有用。
由于某种原因,ckeditor 仅在我第一次加载表单时附加,第二次我收到与 jackboberg 完全相同的错误消息。
我分析了我的代码,发现如果您在“空中”附加 ckeditor,即表单内容仍未放入对话框中,ckeditor 将无法正确附加其绑定。那是因为 ckeditor 附加在“空中”,第二次将它附加在“空中”...噗...由于第一个实例未从 DOM 中正确删除,因此会抛出错误。
这是我的代码引发了错误:
这是有效的修复:
I had exactly the same problem like jackboberg. I was using dynamic form loading into jquery dialogs then attaching various widgets (datepickers, ckeditors etc...).
And I tried all solutions noted above, none of them worked for me.
For some reason ckeditor only attached the first time I loaded form, the second time I got exactly the same error message jackboberg did.
I've analyzed my code and discovered that if you attach ckeditor in "mid-air" that is while form content is still not placed into dialog, ckeditor won't properly attach its bindings. That is since ckeditor is attached in "mid-air", second time you attach it in "mid-air"... poof ... an error is thrown since the first instance was not properly removed from DOM.
This was my code that ptoduced the error:
This is the fix that worked:
我遇到了同样的事情,问题是字数插件初始化时间太长。 30多秒。用户可以单击显示 ckeditor 的视图,然后取消,从而通过 ajax 将新页面加载到 dom 中。该插件抱怨是因为当它准备好将自身添加到 contentWindow 时,iframe 或任何 contentWindow 指向的内容不再可见。您可以通过单击视图然后等待字数统计出现在编辑器的右下角来验证这一点。如果您现在取消,就不会有问题。如果您不等待,您将收到 i.contentWindow is null 错误。要修复这个问题,只需废弃插件即可:
如果您需要单词计数器,请使用计算单词的函数在编辑器上注册粘贴和键盘事件。
I ran into this exact same thing and the problem was that the wordcount plugin was taking too long to initialize. 30+ seconds. The user would click into the view displaying the ckeditor, then cancel, thereby ajax-loading a new page into the dom. The plugin was complaining because the iframe or whatever contentWindow is pointing to was no longer visible by the time it was ready to add itself to the contentWindow. You can verify this by clicking into your view and then waiting for the Word Count to appear in the bottom right of the editor. If you cancel now, you won't have a problem. If you don't wait for it, you'll get the i.contentWindow is null error. To fix it, just scrap the plugin:
If you need a word counter, register for the paste and keyup events on the editor with a function that counts the words.
对于那些使用jquery“适配器”并遇到麻烦(就像我一样)的人来说,超级黑客但可行的解决方案是执行以下操作:
关键点是这部分:
这解决了编辑器文本在下一个不可见的问题打开对话框的时间。我意识到这是非常黑客的,但考虑到其中大部分将用于管理工具,我认为这不像通常那样是一个大问题..并且这是有效的,所以希望它能为某人节省一些时间 ;)
For those using the jquery "adapter" and having trouble (as I was), as super hackish yet working solution is to do something like this:
The key point being this part:
This fixes the problem with the editor text not being visible the next time you open the dialog. I realise this is very hackish, but considering that most of these are going to be used for admin tools, I don't think that's as big a concern as it normally would be.. and this works, so hopefully it will save someone some time ;)
这是 jquery .load() api 和 ckeditor 的完整工作代码,在我的例子中,我将带有 ckeditor 的页面加载到带有一些 jquery 效果的 div 中。我希望它能帮助你。
This is the fully working code for jquery .load() api and ckeditor, in my case I am loading a page with ckeditor into div with some jquery effects. I hope it will help you.
它非常简单。就我而言,我运行了下面的 jquery 方法,该方法将在页面加载期间销毁 ckeditor 实例。这解决了问题并解决了问题 -
JQuery 方法 -
就是这样。我希望它对你有帮助。
干杯,
西里什。
Its pretty simple. In my case, I ran the below jquery method that will destroy ckeditor instances during a page load. This did the trick and resolved the issue -
JQuery method -
That's it. I hope it helps you.
Cheers,
Sirish.
CKeditor 4.2.1
这里有很多答案,但对我来说,我需要更多的东西(也有点脏,所以如果有人可以改进,请这样做)。对于我来说,MODAL 是我的问题。
我使用 Foundation 在模态中渲染 CKEditor。理想情况下,我会在关闭时毁掉编辑器,但我不想惹恼基金会。
我打电话给删除,我尝试了删除和另一种方法,但这就是我最终解决的方法。
我使用文本区域来填充而不是 DIV。
我的解决方案
这是我返回特定格式的方法,您可能不想要这种格式。
CKeditor 4.2.1
There is a lot of answers here but for me I needed something more (bit dirty too so if anyone can improve please do). For me MODALs where my issue.
I was rendering the CKEditor in a modal, using Foundation. Ideally I would have destoryed the editor upon closing, however I didn't want to mess with Foundation.
I called delete, I tried remove and another method but this was what I finally settled with.
I was using textarea's to populate not DIVs.
My Solution
this was my method to return my specific formatting, which you might not want.
试试这个:
Try this: