如何判断CKEditor是否已加载?

发布于 2024-09-13 20:13:52 字数 94 浏览 5 评论 0原文

如何确定 CKEditor 是否已加载?我查看了API文档,但只能找到loaded事件。我想检查 CKEditor 是否已加载,因为如果我第二次加载它,我的文本区域就会消失。

How do I find out if CKEditor is loaded? I've looked through the API docs, but could only find the loaded event. I want to check if CKEditor is loaded, because if I load it a second time, my textareas disapears.

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

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

发布评论

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

评论(7

小嗲 2024-09-20 20:13:52

loaded 事件对我不起作用。 instanceReady 有效:

CKEDitor_loaded = false;

CKEDITOR.on('instanceReady', function(){ CKEditor_loaded = true; }); 

The loaded event didn't work for me. instanceReady worked:

CKEDitor_loaded = false;

CKEDITOR.on('instanceReady', function(){ CKEditor_loaded = true; }); 
画中仙 2024-09-20 20:13:52
var waitCKEDITOR = setInterval(function() {
    if (window.CKEDITOR) {
       clearInterval(waitCKEDITOR);
       //CKEDITOR.replace(...);
    }
}, 100/*milli*/);
var waitCKEDITOR = setInterval(function() {
    if (window.CKEDITOR) {
       clearInterval(waitCKEDITOR);
       //CKEDITOR.replace(...);
    }
}, 100/*milli*/);
那伤。 2024-09-20 20:13:52

我查看了API文档,但只能找到loaded事件。

我不知道是否存在这样的特定属性 - 可能有! - 但您可以使用已加载事件来设置全局标志。这不是很好,但可以完成工作。

// At the top of the script
CKEDitor_loaded = false;

// then later
CKEDITOR.on('loaded', function(){ CKEditor_loaded = true; });

您还可以考虑在 CKEDITOR 中设置一些内容,而不是全局变量:

CKEDITOR.flag_loaded = true;

这会更干净一些。

I've looked through the API docs, but could only find the loaded event.

I don't know whether there exists a specific property for this - there might! - but you could use the loaded event to set a global flag. It's not really nice but would do the job.

// At the top of the script
CKEDitor_loaded = false;

// then later
CKEDITOR.on('loaded', function(){ CKEditor_loaded = true; });

Instead of a global variable, you could also consider setting something inside CKEDITOR:

CKEDITOR.flag_loaded = true;

This would be a bit cleaner.

旧故 2024-09-20 20:13:52

如果实例未准备好,则文本集将被丢弃

在 CkEditor 初始化(此处为版本 4)时,在编辑器准备好处理数据之前,您不应该设置任何数据。

// Initialize this._editor with replace

if (this._editor.status !== "ready") {
    this._editor.on("instanceReady",
        event => {
            event.editor.setData(data);
        });
} else {
    this._editor.setData(data);
}

If instance is not ready, the text set would be discarded

On initialization of the CkEditor (version 4 here), you should never set any data before the editor is ready to handle it.

// Initialize this._editor with replace

if (this._editor.status !== "ready") {
    this._editor.on("instanceReady",
        event => {
            event.editor.setData(data);
        });
} else {
    this._editor.setData(data);
}
爱要勇敢去追 2024-09-20 20:13:52

希望这对某人有帮助。

我还通过 AJAX 加载具有 CKEDITOR 功能的页面片段,因此我遇到了这个问题中概述的许多问题。这是我的解决方案:

function setCk(id){
if(window.CKEDITOR){
    var _instId = CKEDITOR.instances[id];
    if(_instId == undefined){
        CKEDITOR.inline(id);
    }else{
        CKEDITOR.instances[id].destroy();
        CKEDITOR.inline(id);
    }
}

}

在此片段的每个 AJAX 响应中,我通过调用 setCk(textareaId) 将脚本元素注入到头部。诀窍是销毁目标 ID 和目标 ID 的任何先前的 CKEDITOR 实例。每次加载 AJAX 片段后重新初始化 CKEDITOR。

Hope this helps someone.

I also load a page snippet with CKEDITOR functionality via AJAX and as such I have experienced many of the problems outlined in this question. This is my solution:

function setCk(id){
if(window.CKEDITOR){
    var _instId = CKEDITOR.instances[id];
    if(_instId == undefined){
        CKEDITOR.inline(id);
    }else{
        CKEDITOR.instances[id].destroy();
        CKEDITOR.inline(id);
    }
}

}

On each AJAX response for this snippet I inject a script element into the head with a call to setCk(textareaId). The trick being to destroy any previous CKEDITOR instances for the target ID & re-initialise CKEDITOR after each AJAX snippet load.

倒数 2024-09-20 20:13:52

我知道这是一篇非常古老的文章,但在我的研究中它不断出现。我通过 jQuery 动态加载 CKEditor。正如您发现的那样,自从事情开始发生以来,我不想多次加载它。

简单的解决方案:

if (!window.CKEDITOR) {
    // (not loaded yet, your code to load it)
}

I know this is a very old post, but in my research it kept coming up. I am dynamically loading the CKEditor through jQuery. I didn't want to load it multiple times since things start happening, as you found out.

Simple solution:

if (!window.CKEDITOR) {
    // (not loaded yet, your code to load it)
}
无法言说的痛 2024-09-20 20:13:52
//creating instance of ck-editor
var yourInstance = CKEDITOR.instances.yourContainer;
//check instance of your ck-editor 
if(yourInstance){
      //destroy instance
      yourInstance .destroy(true); 
}
// create instance again
CKEDITOR.replace( 'yourContainer' );
//creating instance of ck-editor
var yourInstance = CKEDITOR.instances.yourContainer;
//check instance of your ck-editor 
if(yourInstance){
      //destroy instance
      yourInstance .destroy(true); 
}
// create instance again
CKEDITOR.replace( 'yourContainer' );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文