CKEDITOR:从Javascript中的多个实例名称中获取数据

CKEDITOR: get data from multiple instance names in Javascript

本文关键字:实例 数据 获取 Javascript CKEDITOR      更新时间:2023-09-26

因为我在HTML代码中有多个文本区域,所以我通过Javascript传递id值来检索每个文本区域中的数据。但是,在 JS 函数中,"CKEDITOR.instances.id"并不表示预期的,例如CKEDITOR.instances.editor_1、CKEDITOR.instances.editor_2或CKEDITOR.instances.editor_4,因此,我没有检索到任何数据。任何人都知道如何解决这个问题,请让我。非常感谢。

网页代码:

    <textarea name="edit_1"></textarea>
    <input type="button" value="submit" onClick="getValue('edit_1')" />
    <textarea name="edit_2"></textarea>
    <input type="button" value="submit" onClick="getValue('edit_2')" />
    <textarea name="edit_2"></textarea>
    <input type="button" value="submit" onClick="getValue('edit_3')" />

JS代码:

    var getValue = function(id) {
        var content = CKEDITOR.instances.id.getData();
        alert(content);
    };

尝试在 id 之间添加 []

var getValue = function(id) {
    var content = CKEDITOR.instances[id].getData();
    alert(content);
};

我不得不做这样的事情,因为我将事件绑定到具有多个实例的操作。并尝试获取数据,但它总是为除最后一个之外的任何一个返回 null......使用事件(e.editor)虽然有效。

var editors = CKEDITOR.instances;
    for (var x in editors) {
      if (editors[x]) {
        var thisName = editors[x].name;
        if (editors[thisName]) {
          editors[thisName].on('focus', function (e) {
            socket.emit('ckeditor_field_type_edit', user, e.editor.name);
          });
          editors[thisName].on('key', function (e) {
            var data = e.editor.getData();
            socket.emit('ckeditor_field_type_typing', user, e.editor.name, data);
          });
          editors[thisName].on('blur', function (e) {
            var data = e.editor.getData();
            setTimeout(function () {
              socket.emit('ckeditor_field_type_edit_finish', user, e.editor.name, data);
            }, 1000);
          });
        }
      }
    }