12

我尝试将 CKEditor 5 设置为 more than one <textarea>,但只有第一个有效。

这是代码:

<script src="https://cdn.ckeditor.com/ckeditor5/1.0.0-alpha.2/classic/ckeditor.js"></script>
<textarea name="content0" class="editor" id="c0">This is some sample content.</textarea>
<textarea name="content1" class="editor" id="c1">This is some sample content.</textarea>
<textarea name="content2" class="editor" id="c2">This is some sample content.</textarea>
<script>ClassicEditor.create(document.querySelector('.editor'));</script>

结果如下: 在此处输入图像描述

为什么只有第一?

4

3 回答 3

28

document.querySelector()返回第一个匹配的元素。你需要document.querySelectorAll()

<script src="https://cdn.ckeditor.com/ckeditor5/1.0.0-alpha.2/classic/ckeditor.js"></script>
<textarea name="content0" class="editor" id="c0">This is some sample content.</textarea>
<textarea name="content1" class="editor" id="c1">This is some sample content.</textarea>
<textarea name="content2" class="editor" id="c2">This is some sample content.</textarea>
<script>
var allEditors = document.querySelectorAll('.editor');
for (var i = 0; i < allEditors.length; ++i) {
  ClassicEditor.create(allEditors[i]);
}
</script>
于 2018-01-08T07:09:52.010 回答
1

我更喜欢使用 foreach 而不是像@Wizard's answer这样的循环

document.querySelectorAll('[data-init-plugin="ckeditor"]').forEach(function (val) {
    ClassicEditor
        .create(val, {
            toolbar: ['bold', 'italic', 'link', 'bulletedList', 'numberedList'],
        })
        .catch(error => {
            console.log(error);
        });
});

于 2021-04-07T22:42:26.203 回答
0

这个解决方案对我有用

<script>
    CKEDITOR.on('instanceReady', function(event) {
        var editor = event.editor;
        editor.on('change', function(event) {
            // Sync textarea
            this.updateElement();
            for (var i in CKEDITOR.instances) {
            CKEDITOR.instances['ckeditor' + i].on('change', function() 
            { CKEDITOR.instances['ckeditor' + i].updateElement() });
            }
        });
    });
</script>
于 2021-09-23T11:09:57.423 回答