CKEditor 的 SetData 在版本 4 中不起作用 -
CKEditor's SetData doesn't work in version 4 -
我正在升级到 CKEditor 4.5.8 版。我使用 jQuery 访问 CKEditor API。
编辑器的文本区域没有更新。我需要设置配置选项吗?
Html:
<textarea name="mck_1" id="mck_1"> Enter text. </textarea>
<script> CKEDITOR.replace( 'mck_1' ); </script>
Javascript:
CKEDITOR.instances.mck_1.setData("Hello");
我该如何调试它?我必须明确更新 display/textarea 吗?
如果我尝试手动更新 Firebug 中的文本区域,它会显示 "undefined"。
CKEDITOR.instances.mck_1.setData("hi");
undefined
在 Whosebug 中有很多 "setData not working" 有关 CKEditor 过去版本的问题,但 none 那里的建议解决方案对我有用。
求助!
MMiz
编辑:我意识到 textarea 上的第一个 "setData" 确实有效。接下来的第二个不显示。从此thread
1: http://ckeditor.com/forums/Support/Second-Time-jQuery-Doesnt-Work 好像有一些时间问题。但这就是我所得到的。
事实证明,答案在 CKEditor 文档中(是的,我知道 - 首先是 RTFM)。此行为的根本原因是 setData 方法是异步的,因此您需要相应地进行编码。
http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-setData
引用:"Note that this method is asynchronous. The callback parameter must be used if interaction with the editor is needed after setting the data."
在我的例子中,因为我试图连续两次设置数据,所以我肯定需要有一个回调。现在可以使用了。
$('textarea').each(function () {
var $textarea = $(this);
CKEDITOR.instances[$textarea.attr('name')].setData($textarea.val());
});
代替从 ckeditor 获取数据并设置回编辑器使用 textarea 并迭代并获取所有数据并设置到每个 ckeditor,,,在迭代之前创建一个 CKEDITOR 实例
这个 -> http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-setData
替换:
CKEDITOR.instances.editor1.updateElement();
与:
CKEDITOR.instances['editor1'].setData(response, {
callback: function() {
this.checkDirty(); // true
}
} );
我目前正在使用 ckeditor5,但以上答案都不适合我。
基于document,您可以使用以下代码行操作ckeditor的内部html:
// A reference to the editor editable element in the DOM.
const domEditableElement = document.querySelector( '.ck-editor__editable' );
// Get the editor instance from the editable element.
const editorInstance = domEditableElement.ckeditorInstance;
// Use the editor instance API.
editorInstance.setData( '<p>Hello world!<p>' );
我正在升级到 CKEditor 4.5.8 版。我使用 jQuery 访问 CKEditor API。
编辑器的文本区域没有更新。我需要设置配置选项吗?
Html:
<textarea name="mck_1" id="mck_1"> Enter text. </textarea>
<script> CKEDITOR.replace( 'mck_1' ); </script>
Javascript:
CKEDITOR.instances.mck_1.setData("Hello");
我该如何调试它?我必须明确更新 display/textarea 吗?
如果我尝试手动更新 Firebug 中的文本区域,它会显示 "undefined"。
CKEDITOR.instances.mck_1.setData("hi");
undefined
在 Whosebug 中有很多 "setData not working" 有关 CKEditor 过去版本的问题,但 none 那里的建议解决方案对我有用。
求助!
MMiz
编辑:我意识到 textarea 上的第一个 "setData" 确实有效。接下来的第二个不显示。从此thread
1: http://ckeditor.com/forums/Support/Second-Time-jQuery-Doesnt-Work 好像有一些时间问题。但这就是我所得到的。
事实证明,答案在 CKEditor 文档中(是的,我知道 - 首先是 RTFM)。此行为的根本原因是 setData 方法是异步的,因此您需要相应地进行编码。
http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-setData
引用:"Note that this method is asynchronous. The callback parameter must be used if interaction with the editor is needed after setting the data."
在我的例子中,因为我试图连续两次设置数据,所以我肯定需要有一个回调。现在可以使用了。
$('textarea').each(function () {
var $textarea = $(this);
CKEDITOR.instances[$textarea.attr('name')].setData($textarea.val());
});
代替从 ckeditor 获取数据并设置回编辑器使用 textarea 并迭代并获取所有数据并设置到每个 ckeditor,,,在迭代之前创建一个 CKEDITOR 实例
这个 -> http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-setData
替换:
CKEDITOR.instances.editor1.updateElement();
与:
CKEDITOR.instances['editor1'].setData(response, {
callback: function() {
this.checkDirty(); // true
}
} );
我目前正在使用 ckeditor5,但以上答案都不适合我。 基于document,您可以使用以下代码行操作ckeditor的内部html:
// A reference to the editor editable element in the DOM.
const domEditableElement = document.querySelector( '.ck-editor__editable' );
// Get the editor instance from the editable element.
const editorInstance = domEditableElement.ckeditorInstance;
// Use the editor instance API.
editorInstance.setData( '<p>Hello world!<p>' );