对不起,我的英语不好。
有这么一段代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.ckeditor.com/ckeditor5/43.1.0/ckeditor5.css" />
<body>
<div id="addtext">button</div>
<div id="inform">
<div class="editor">
<p>Hello from CKEditor 5!</p>
</div>
</div>
</body>
<script type="importmap">
{
"imports": {
"ckeditor5": "https://cdn.ckeditor.com/ckeditor5/43.1.0/ckeditor5.js",
"ckeditor5/": "https://cdn.ckeditor.com/ckeditor5/43.1.0/"
}
}
</script>
<script type="module">
import {
ClassicEditor,
Essentials,
Bold,
Italic,
Font,
Paragraph
} from 'ckeditor5';
// При загрузки страницы
ClassicEditor
.create(document.querySelector('.editor'), {
plugins: [Essentials, Bold, Italic, Font, Paragraph],
toolbar: [
'undo', 'redo', '|', 'bold', 'italic',
]
})
.then( /* ... */)
.catch( /* ... */);
$("#addtext").on("click", function () {
var a = $('.editor').html();
alert (a);
})
</script>
我更改了ckeditor5表单中的文本并单击addtext,结果,我在警报中得到了旧文本。如何在 ckeditor5 表单中获取新文本?
我需要动态更改文本。
只需使用 CKEditor API
editor.getData()
let editor;
ClassicEditor
.create(document.querySelector('.editor'), {
plugins: [Essentials, Bold, Italic, Font, Paragraph],
toolbar: [
'undo', 'redo', '|', 'bold', 'italic',
]
})
.then(newEditor => { editor = newEditor; })
.catch( /* ... */);
$("#addtext").on("click", function () {
var a = editor.getData();
alert (a);
})