CKEditor:如何销毁模糊实例?

发布于 2024-12-06 02:24:58 字数 654 浏览 1 评论 0原文

我有一个 html 页面,上面有多个 div 元素。每次用户单击 div 时,它都会即时替换为 CKEditor

$('.editable').click(function() {
    editor = CKEDITOR.replace(this);
});

现在,如果 CKEditor 实例失去焦点(模糊事件),我需要通过 ajax 将内容发布到单独的脚本(如果发生更改)并销毁此实例:

$('.editable').click(function() {
    editor = CKEDITOR.replace(this);
    editor.on('blur', function(e)
    {
        if (e.editor.checkDirty())
           // get data with e.editor.getData() and do some ajax magic 

        e.editor.destroy();
    });
});

但是此示例不起作用,因为我不知道为什么,之前会调用 destory() checkDirty()。我怎样才能让它发挥作用?

I have a single html page with multiple div elements on it. Each time a user clicks on on a div, it is replaced with an CKEditor on the fly:

$('.editable').click(function() {
    editor = CKEDITOR.replace(this);
});

Now, if the CKEditor instance loses its focus (blur event), I need to post the content to a separate script via ajax (if something changed) and destroy this instance:

$('.editable').click(function() {
    editor = CKEDITOR.replace(this);
    editor.on('blur', function(e)
    {
        if (e.editor.checkDirty())
           // get data with e.editor.getData() and do some ajax magic 

        e.editor.destroy();
    });
});

But this example won't work because, I don't know why, destory() will be called before checkDirty(). How can I get this working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水染的天色ゝ 2024-12-13 02:24:58

如果将 destroy() 放在 if() 语句中怎么样?您可以有一个 else 子句,如果没有任何更改,则调用 destroy。如果某些内容发生变化,您可以在数据传输后在 if 子句中调用 destroy()

$('.editable').click(function() {
  editor = CKEDITOR.replace(this);
  editor.on('blur', function(e)
  {
    if (e.editor.checkDirty()) {
       // get data with e.editor.getData() and do some ajax magic
       if ( dataTransferComplete ) {
         e.editor.destroy();
       }

     } else {
       e.editor.destroy();
     }
  });
});

或者您可以在调用destroy() 之前检查变量。数据传输完成后并在 else 子句中将该变量设置为 true,这样在检查更改并传输任何更新的数据之前不会调用 destroy()

$('.editable').click(function() {
  editor = CKEDITOR.replace(this);
  editor.on('blur', function(e)
  {
    var okToDestroy = false;

    if (e.editor.checkDirty()) {
       // get data with e.editor.getData() and do some ajax magic
       okToDestroy = true;
     } else {
       okToDestroy = true;
     }

    if (okToDestroy )
      e.editor.destroy();

  });
});

这是一个大纲,我还没有测试代码,但如果显示了概念。

祝你好运,

How about if you put the destroy() inside the if() statement? You could have an else clause that invokes destroy if nothing has changed. If something has changed, you can invoke destroy() within the if clause once the data has been transfered.

$('.editable').click(function() {
  editor = CKEDITOR.replace(this);
  editor.on('blur', function(e)
  {
    if (e.editor.checkDirty()) {
       // get data with e.editor.getData() and do some ajax magic
       if ( dataTransferComplete ) {
         e.editor.destroy();
       }

     } else {
       e.editor.destroy();
     }
  });
});

Or you could check a variable before invoking destroy(). Set that variable to true after the data transfer has been completed and in the else clause, that way destroy() won't be invoked until you've checked for changes and transfered any updated data.

$('.editable').click(function() {
  editor = CKEDITOR.replace(this);
  editor.on('blur', function(e)
  {
    var okToDestroy = false;

    if (e.editor.checkDirty()) {
       // get data with e.editor.getData() and do some ajax magic
       okToDestroy = true;
     } else {
       okToDestroy = true;
     }

    if (okToDestroy )
      e.editor.destroy();

  });
});

This is an outline, I haven't tested the code, but if shows the concept.

Be Well,
Joe

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文