如何获得CKEditor 5的价值? [英] How to get value of CKEditor 5?

查看:615
本文介绍了如何获得CKEditor 5的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够返回CKEditor textarea的值,并且还可以在其中写入我的文本。

I want to be able to return the value of the CKEditor textarea, and also write my text inside it.

我使用了CKEditor 5 CDN。首先,我的textarea的代码工作正常

I used CKEditor 5 CDN. First this my code for the textarea it works fine

<script src="https://cdn.ckeditor.com/ckeditor5/1.0.0-alpha.1/classic/ckeditor.js"></script>

<textarea class="inputStyle" id="editor" name="content" placeholder="Write your email.."></textarea>
<script>ClassicEditor.create(document.querySelector('#editor')).catch( error => { console.error( error );  } ); </script>

我曾经在CKEditor之前从textarea获取数据:

I used to get the data from the textarea before the CKEditor by:

var text = $('textarea#editor').val();

并设定数据:

and set the data by:

$('textarea#editor').html("");

但我现在失去了吗?我尝试了很多方法...什么是正确的方法?

but i'm lost now? i tried many ways... what is the correct way?

推荐答案

您需要获取或保存创建的编辑器,然后使用getData()函数。
您可以在创建时添加一个.then来保存您的编辑器。

You need to get or save the editor created and then use the getData() function. You can add a .then on creation to save your editor.

    var myEditor;

    ClassicEditor
        .create( document.querySelector( '#editor' ) )
        .then( editor => {
            console.log( 'Editor was initialized', editor );
            myEditor = editor;
        } )
        .catch( err => {
            console.error( err.stack );
        } );

然后使用

and then get data using

myEditor.getData();

这篇关于如何获得CKEditor 5的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆