2

大家好,我的屏幕上有一个 CKeditor ckeditor 正在显示,但是当我尝试获取编辑器的实例时,我的值为 null 或 undefined 如何获取 ckeditor 实例,任何人都可以在这里帮助我

function editor() {
    //i have null here
    for (name in CKEDITOR.instances) {
        CKEDITOR.instances[name].destroy()
    }

    var editor = CKEDITOR.instances.editor1;//i have null here
    editor.on('key', function () {
        var data = editor.getData();        
    });
}

$(document).ready(function () {    
    var editorShort = CKEDITOR.editor.replace('popup_editor1');
    editor();
});
4

2 回答 2

6

可能是实例变量当时只是未定义。根据你真正想用编辑器做什么,我建议你在 instanceReady 中执行键绑定和其他类似的事情,这是一个 CKEditor 事件。

$(document).ready(function () {  
    // You can define it before replacing the editor
    CKEDITOR.on('instanceReady', function(e){
        // Do your bindings / other actions here
        // To access the editor that this event has fired on:
        const editor = e.editor;
    });

   // replace editor
    const editorVar = CKEDITOR.editor.replace('popup_editor1');
});

请参阅http://docs.ckeditor.com/#!/api/CKEDITOR-event-instanceReady上的文档


或者如果你不使用 jQuery

document.addEventListener("DOMContentLoaded", () => {
    // You can define it before replacing the editor
    CKEDITOR.on('instanceReady', function(e){
        // Do your bindings / other actions here
        // To access the editor that this event has fired on:
        const editor = e.editor;
    });

   // replace editor
    const editorVar = CKEDITOR.editor.replace('popup_editor1');
});
于 2013-04-08T05:17:09.433 回答
0

Nenotlep 的回答很好,我只是在添加一些东西来帮助像我这样的人。

<script type="text/javascript">
$(document).ready(function () { 
CKEDITOR.on('instanceReady', function(evt){
  console.log("instance is ready");
  for (var instance in CKEDITOR.instances)
            CKEDITOR.instances[instance].updateElement(); 

// 现在如果你做 console.log(instance) 它将给 editor 或 id_your_editor ,使用这个值

  var slug= $('#slugfield').val()
  var e=CKEDITOR.instances.id_Your_Message
  e.on('keyup', function(e){
  var content = CKEDITOR.instances[instance].getData()
  $.ajax({
      type: "POST",
      url: "{% url 'edit-update' %}",
      data: {'slug':newslug,'code':content},
      success: function (data) {
        console.log("instance is");
        console.log(instance);
      },
      error: function(data) {
          console.log("error");
      }
  });

});
});
});
</script>
于 2019-12-31T16:30:31.050 回答