javascript 如何将数据加载到ckeditor onload
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14150604/
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
How to load data into ckeditor onload
提问by NaN
Is it possible to load data into the ckeditor without using JQuery? I would like to use an inline script tag for this if possible. All of the examples I can find deal with Jquery and that isn't optimal in my case.
是否可以在不使用 JQuery 的情况下将数据加载到 ckeditor 中?如果可能,我想为此使用内联脚本标记。我能找到的所有示例都处理 Jquery,但在我的情况下这不是最佳的。
What I have so far is the example I found on CKs site:
到目前为止,我在 CKs 网站上找到的示例:
CKEDITOR.instances.editor1.setData( '<p>Some other editor data.</p>', function()
{
this.checkDirty(); // true
});
I've tried to use this with an inline script tag but it crashes the editor. If I use this in JQuery, it will work as expected.
我尝试将它与内联脚本标记一起使用,但它会使编辑器崩溃。如果我在 JQuery 中使用它,它将按预期工作。
回答by Patrick Jones
Here is an example using CKEDITOR API without using jQuery. Please observe the call to replace function which initializes the "editor1" instance of the CKEDITOR with the textarea content. I think that would be the easiest way to populate the content onload, but if that will not suffice, you can see I've used the code in your question to populate the CKEDITOR with "Some other editor data."
这是一个使用 CKEDITOR API 而不使用 jQuery 的示例。请注意替换函数的调用,该函数使用 textarea 内容初始化 CKEDITOR 的“editor1”实例。我认为这将是填充加载内容的最简单方法,但如果这还不够,您可以看到我已经使用您问题中的代码用“其他一些编辑器数据”填充 CKEDITOR。
HTML:
HTML:
<textarea id="editor1">
Hello, world!
</textarea>?
Javascript:
Javascript:
CKEDITOR.replace( 'editor1', {
toolbar: 'Basic',
uiColor: '#9AB8F3'
});
CKEDITOR.instances.editor1.setData( '<p>Some other editor data.</p>', function()
{
this.checkDirty(); // true
});
?
?
回答by miks87
I know this question is old but I've just figured out my solution and I think is cleaner so I'm adding my two cents here for whoever might have the same problem in the future.
我知道这个问题很老,但我刚刚想出了我的解决方案,我认为更清晰,所以我在这里为将来可能遇到同样问题的人增加两分钱。
CKEditor automatically sets the content of the new textarea using what is originally within the old, replaced one. In other words, it's sufficient to change the content of the textarea and only then call the replace command. This would be the code:
CKEditor 使用原来在旧的、被替换的文本中的内容自动设置新文本区域的内容。换句话说,只要改变textarea的内容,然后调用replace命令就足够了。这将是代码:
document.getElementById("editor1").value = "<p>Some other editor data.</p>";
CKEDITOR.replace("editor1");
Done :)
完毕 :)
回答by Joseph Ajibodu
Transforms input data into HTML to be loaded into the editor. While the editor is able to handle non-HTML data (like BBCode), it can only handle HTML data at runtime. The role of the data processor is to transform the input data into HTML through this function.
将输入数据转换为 HTML 以加载到编辑器中。虽然编辑器能够处理非 HTML 数据(如 BBCode),但它只能在运行时处理 HTML 数据。数据处理器的作用就是通过这个函数把输入的数据转换成HTML。
See source
查看来源
// Tranforming BBCode data, with a custom BBCode data processor available.
var data = 'This is [b]an example[/b].';
var html = editor.dataProcessor.toHtml( data ); // '<p>This is <b>an example</b>.</p>'