如何获得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();