简体   繁体   English

如何将数据加载到ckeditor onload

[英]How to load data into ckeditor onload

Is it possible to load data into the ckeditor without using JQuery?是否可以在不使用 JQuery 的情况下将数据加载到 ckeditor 中? 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,但在我的情况下这不是最佳的。

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 中使用它,它将按预期工作。

Here is an example using CKEDITOR API without using jQuery.这是一个使用 CKEDITOR API 而不使用 jQuery 的示例。 Please observe the call to replace function which initializes the "editor1" instance of the CKEDITOR with the textarea content.请注意替换函数的调用,该函数使用 textarea 内容初始化 CKEDITOR 的“editor1”实例。 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。

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
});

http://jsfiddle.net/cDzqp/ http://jsfiddle.net/cDzqp/

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. CKEditor 使用原来在旧的、被替换的文本中的内容自动设置新文本区域的内容。 In other words, it's sufficient to change the content of the textarea and only then call the replace command.换句话说,改变textarea的内容,然后调用replace命令就足够了。 This would be the code:这将是代码:

document.getElementById("editor1").value = "<p>Some other editor data.</p>";
CKEDITOR.replace("editor1");

Done :)完毕 :)

If someone is looking for solution to append content to existing content in CKEditor this will help.如果有人正在寻找将内容附加到 CKEditor 中现有内容的解决方案,这将有所帮助。 - I am using CKEditor 4.15.0 - 我正在使用 CKEditor 4.15.0

CKEDITOR.replace( 'editor1', {
toolbar: 'Basic',
uiColor: '#9AB8F3'});

CKEDITOR.instances.editor1.setData( CKEDITOR.instances.editor1.getData + 'New Content Here', function()
{
    this.checkDirty();  // true
});

modified as per my requirement, based on answer by Patrick Jones根据我的要求修改,基于 Patrick Jones 的回答

Thanks谢谢

Transforms input data into HTML to be loaded into the editor.将输入数据转换为 HTML 以加载到编辑器中。 While the editor is able to handle non-HTML data (like BBCode), it can only handle HTML data at runtime.虽然编辑器能够处理非 HTML 数据(如 BBCode),但它只能在运行时处理 HTML 数据。 The role of the data processor is to transform the input data into HTML through this function.数据处理器的作用就是通过这个函数将输入的数据转换成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>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM