CKEditor setData() 函数每次都不起作用

问题描述 投票:0回答:1

我在页面中归档的文本区域上使用 CKEditor 4,当我打开页面中的网格形式的数据时,我将 setData() 函数放在 on instanceReady 事件内的文本区域内的内容,但有时它不进入instanceReady事件函数内部。这是我的js代码:

function prepCkEditor() {
  var oFCKeditor = CKEDITOR.instances.EMAIL_DESC_TEMPLATETextBox;
  if (!oFCKeditor) {
    CKEDITOR.replace('EMAIL_DESC_TEMPLATETextBox', {
        language: editorlang,
        toolbar: 'helpsi',
        skin: 'moono',
        scayt_autoStartup: CONFIGURAZIONE.enableSpellCheck,
        scayt_sLang: $('#hfUSER_LANGEXTENDED').val().replace('-', '_'),
        scayt_disableOptionsStorage: 'lang'
    });
  }
}

prepCkEditor();

var oFCKeditor = CKEDITOR.instances.EMAIL_DESC_TEMPLATETextBox;
if (oFCKeditor) {
   console.log(oFCKeditor.status);
   oFCKeditor.on('instanceReady', function () {
      oFCKeditor.setData(template.TemplateBody);
   });
}

放在 on instanceReady 行之前的 console.log 语句打印“ready”,因此看起来编辑器已完全加载,但它没有进入函数内部来设置数据。这可能是什么问题?

如果我在浏览器开发工具中检查调试,它会起作用,但打印的状态已卸载

javascript ckeditor
1个回答
0
投票

这是因为当你到达这行代码时,有时它已经准备好了,有时还没有准备好。

您应该在使用

on
属性创建实例时附加事件。

function prepCkEditor() {
  var oFCKeditor = CKEDITOR.instances.EMAIL_DESC_TEMPLATETextBox;
  if (!oFCKeditor) {
    CKEDITOR.replace('EMAIL_DESC_TEMPLATETextBox', {
      language: editorlang,
      toolbar: 'helpsi',
      skin: 'moono',
      scayt_autoStartup: CONFIGURAZIONE.enableSpellCheck,
      scayt_sLang: $('#hfUSER_LANGEXTENDED').val().replace('-', '_'),
      scayt_disableOptionsStorage: 'lang',
      on: {
        instanceReady: function () {
          console.log(oFCKeditor.status);
          oFCKeditor.setData(template.TemplateBody);
        }
      }
    });
  }
}

© www.soinside.com 2019 - 2024. All rights reserved.