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

发布于 2024-09-16 21:02:50 字数 227 浏览 4 评论 0原文

我想知道如何使用 Javascript 在 CKEditor 中设置值?

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

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

但是,这两种方法都在没有应用编辑器的情况下起作用。有什么办法可以让编辑器做到这一点吗?

I am wondering how I can set a value in CKEditor using 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

软糖 2024-09-23 21:02:50

使用 CKEditor 方法 setData()

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

Use the CKEditor method setData():

CKEDITOR.instances[**fieldname**].setData(**your data**)
久光 2024-09-23 21:02:50

insertHtml()insertText() 方法会将数据插入到编辑器窗口中,添加到已有的内容中。

但是,要替换整个编辑器内容,请使用 setData()< /代码>

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

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

昨迟人 2024-09-23 21:02:50

使用 insertHtml()insertText() 方法。

Use insertHtml() or insertText() method.

季末如歌 2024-09-23 21:02:50

试试这个

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

Try This

CKEDITOR.instances['textareaId'].setData(value);
苏大泽ㄣ 2024-09-23 21:02:50

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

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

这里->
CKEDITOR ->您的编辑器名称,
mail_msg ->您的文本区域的 ID(您将 ckeditor 绑定到该文本区域),
obj["template"]->是你想要绑定的值

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

拥醉 2024-09-23 21:02:50
<textarea id="editor1" name="editor1">This is sample text</textarea>

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

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

</script>

让我们试试这个..

更新:

设置数据:

首先创建实例::

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

然后,

editor.setData('your data');

editor.insertHtml('your html data');

editor.insertText('your text data');  

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

editor.getData();

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

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

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

<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.

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

These are the possible ways that I know in CKEditor

睡美人的小仙女 2024-09-23 21:02:50

由于现在 CKEditor 4+ 已推出,我们必须使用它。ekeditor 4 setData 文档

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

其中editor1 是textarea Id

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

获取数据

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

并使用CKedtor 4文档

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

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

Where editor1 is textarea Id.

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

and to get data use

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

Ckedtor 4 documentation

且行且努力 2024-09-23 21:02:50

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

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

起风了 2024-09-23 21:02:50

我尝试过这个并为我工作。

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

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);
    });
},
━╋う一瞬間旳綻放 2024-09-23 21:02:50

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

另请注意,即使您这样做,然后使用 getData() 再次获取该数据,CKEditor 也会重新插入换行符。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文