我在 JQuery UI 对话框中创建 CKEditor 的多个实例时遇到问题。该对话框通过 AJAX 加载远程表单,因此目标是能够关闭和重新打开该对话框并拥有一个新的编辑器实例。使用默认选项,当重新打开对话框时,它会给出一个错误,指出具有该名称的编辑器已经存在。所以我尝试了几种破坏编辑器实例的方法,它们都导致了同样的问题。重新加载编辑器时,文本区域显示为空并且按钮不起作用。
目前我正在使用这种销毁实例的方法:
var instance = CKEDITOR.instances['test'];
if (instance) { CKEDITOR.remove(CKEDITOR.instances['test']); }
我用几个可供下载的简单 html 文件重现了这个问题 here .
编辑: 我刚刚尝试使用两个远程文件和一个具有不同名称的文本区域,但我遇到了同样的问题。当一个对话框打开然后关闭时,另一个对话框打开时有一个“空”CKEditor。
此外,显然这只是 Safari 中的一个问题。
这是我所做的:
var CKeditors = {};
function loadEditors() {
var $editors = $("textarea.ckeditor");
if ($editors.length) {
$editors.each(function() {
var editorID = $(this).attr("id");
if(CKeditors[editorID]){
CKeditors[editorID].destroy();
CKeditors[editorID] = null;
}
var dst = editorID+'-element';
var html = '';
if( $(this).val() ){
html = $(this).val();
}
CKeditors[editorID] = CKEDITOR.appendTo(dst, {}, html);
});
$("textarea.ckeditor").hide();
}
}
function updateCKEditors() {
for(x in CKeditors){
$("#"+x).val(CKeditors[x].getData());
}
}
然后在 ajax 成功之后我正在做
loadEditors()
在表单提交之前(例如使用ajax):
updateCKEditors()
您需要 jQuery 才能使其正常工作。这是针对 zend_forms 的,但经过几次更正后也应该可以正常工作。玩“dst”来做到这一点。