如何使用 Javascript 在 CKEditor 中设置值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3610010/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:25:28  来源:igfitidea点击:

How do I set a value in CKEditor with Javascript?

javascriptckeditor

提问by dcp3450

I am wondering how I can set a value in CKEditorusing Javascript?

我想知道如何在CKEditor使用 Javascript 时设置一个值?

I have tried the following, but neither of them work...

我尝试了以下方法,但它们都不起作用......

document.[form name].[textarea name].value=data;
$('#textareaID').val(data);

However, both these work without the editor applied. Is there a way I can do this with the editor?

但是,这两项工作都没有应用编辑器。有没有办法用编辑器做到这一点?

回答by efeyc

Use the CKEditor method setData():

使用 CKEditor 方法setData()

CKEDITOR.instances[**fieldname**].setData(**your data**)

回答by Wick

The insertHtml()and insertText()methods will insert data into the editor window, adding to whatever is there already.

insertHtml()insertText()方法将数据插入到编辑器窗口,添加到任何有没有了。

However, to replace the entire editor content, use setData().

但是,要替换整个编辑器内容,请使用setData().

回答by Anpher

Use insertHtml()or insertText()method.

使用insertHtml()insertText()方法。

回答by Sachin

I have used the below code and it is working fine as describing->

我使用了下面的代码,它在描述中工作正常->

CKEDITOR.instances.mail_msg.insertText(obj["template"]);

Here-> CKEDITOR->Your editor Name, mail_msg-> Id of your textarea(to which u bind the ckeditor), obj["template"]->is the value that u want to bind

这里CKEDITOR- > ->你的编辑器名称, mail_msg->你的textarea的ID(你绑定ckeditor), obj["template"]->是你想要绑定的值

回答by Sunil kumar

Try This

尝试这个

CKEDITOR.instances['textareaId'].setData(value);

回答by Mari Selvan

<textarea id="editor1" name="editor1">This is sample text</textarea>

<div id="trackingDiv" ></div>

<script type="text/javascript">
    CKEDITOR.replace( 'editor1' );

</script>

Let try this..

让我们试试这个..

Update :

更新 :

To set data :

设置数据:

Create instance First::

首先创建实例::

var editor = CKEDITOR.instances['editor1'];

Then,

然后,

editor.setData('your data');

or

或者

editor.insertHtml('your html data');

or

或者

editor.insertText('your text data');  

And Retrieve data from your editor::

并从您的编辑器中检索数据::

editor.getData();

If change the particular para HTML data in CKEditor.

如果在 CKEditor 中更改特定的 para HTML 数据。

var html = $(editor.editable.$);
$('#id_of_para',html).html('your html data');

These are the possible ways that I know in CKEditor

这些是我在 CKEditor 中知道的可能方式

回答by Deva

As now to day CKEditor 4+ launched we have to use it.ekeditor 4 setData documentation

就像现在 CKEditor 4+ 推出一样,我们必须使用它。ekeditor 4 setData 文档

CKEDITOR.instances['editor1'].setData(value);

Where editor1is textarea Id.

editor1textarea Id在哪里。

Old methods such as insertHtml('html data')and insertText('text data')also works fine.

旧方法,例如insertHtml('html data')insertText('text data')也可以正常工作。

and to get data use

并获得数据使用

var ckdata =  CKEDITOR.instances['editor1'].getData();
var data = CKEDITOR.instances.editor1.getData();

Ckedtor 4 documentation

Ckedtor 4 文档

回答by Satya Prakash

Sets the editor data. The data must be provided in the raw format (HTML). CKEDITOR.instances.editor1.setData( 'Put your Data.' ); refer this page

设置编辑器数据。必须以原始格式 (HTML) 提供数据。CKEDITOR.instances.editor1.setData('把你的数据。'); 参考这个页面

回答by bcr

Take care to strip out newlines from any string you pass to setData().Otherwise an exception gets thrown.

注意从传递给的任何字符串中去除换行符,setData().否则会引发异常。

Also note that even if you do that, then subsequently get that data again using getData(),CKEditor puts the line breaks back in.

另请注意,即使您这样做,随后使用getData(),CKEditor再次获取该数据也会将换行符放回原处。

回答by Dhanushka Udayanga

I tried this and worked for me.

我试过这个并为我工作。

success: function (response) {
    document.getElementById('packageItems').value = response.package_items;

    ClassicEditor
    .create(document.querySelector('#packageItems'), {
        removePlugins: ['dragdrop']
    })
    .then(function (editor) {
        editor.setData(response.package_items);
    })
    .catch(function (err) {
        console.error(err);
    });
},