繁体   English   中英

CKeditor 实例为空还是未定义?

[英]CKeditor instance is null or undefined?

大家好,我的屏幕上有一个 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();
});

可能是实例变量当时只是未定义。 根据您真正想用编辑器做什么,我建议您在 instanceReady 中执行 on 键绑定和其他此类操作,这是一个 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');
});

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM