簡體   English   中英

CKEditor無法從iframe獲取當前實例

[英]CKEditor cannot get current instance from iframe

CKEditor 4或以上

我有表單> textarea啟用了CKEditor並正常運行。

我在模態對話框中有iframe,iframe內部是帶有insert_media()javascript函數的按鈕。

function insert_media( element ) {
    // get element html decode
    element = htmlspecialchars_decode( element, 'ENT_QUOTES' );
    // htmlspecialchars_decode is external function.

    // CKEditor insert element ---------------------------------------------
    // use .insertElement()
    var CKEDITOR = window.parent.CKEDITOR;
    var element = CKEDITOR.dom.element.createFromHtml(element);

    // body_value is name of textarea
    // this code only works with specific textarea NOT current active textarea
    //CKEDITOR.instances.body_value.insertElement(element);

    var current_instance_ckeditor = window.parent.test_current();
    // CKEditor insert element ---------------------------------------------

    // close modal dialog at parent window
    window.parent.close_dialog();

    // done
    return false;
}// insert_media

這是主頁html中的javascript

function close_dialog() {
    $('#media-modal').modal('hide');
}// close_dialog


function test_current() {
    console.log( CKEDITOR.currentInstance.name );
}

問題是我無法通過insertElement命令獲取當前活動的CKEditor插入元素。

CKEDITOR.currentInstance未定義或為null

window.parent.CKEDITOR.currentInstance未定義或為null

如何從iframe 獲取當前活動的CKEditor


測試文件: http//www.megafileupload.com/en/file/420060/test-ckeditor-zip.html

如果CKEDITOR.currentInstancenull/undefined ,則沒有編輯器實例處於活動狀態。 這意味着您將焦點從編輯器移動到未被識別為其部分的位置。

但是,如果您正在使用CKEditor的對話框(是嗎?),編輯器實例應始終處於活動狀態,此時將打開此對話框。 如果這是你的情況,那么你需要為我們提供一個工作實例,因為很難猜出什么是錯誤的。

第二個選項是您不使用CKEditor的對話框,然后您必須注意將該iframe注冊到CKEditor的focusManager ,盡管這很棘手,因此您不應該使用第三方與CKEditor的對話框。

編輯當我單擊“測試”按鈕時, test_current函數正常工作,但必須集中編輯器。 但是從你點擊按鈕編輯器的那一刻起200ms之后,你將無法從currentInstance獲得它。 為了避免在單擊按鈕時模糊編輯器,您需要在focusManagers注冊它(兩個編輯器都將在兩個編輯器中使用)。

我會這樣做的

    var ck_instance_name = false;
    for ( var ck_instance in CKEDITOR.instances ){
        if (CKEDITOR.instances[ck_instance].focusManager.hasFocus){
            ck_instance_name = ck_instance;
            return ck_instance_name;
        }
    }

正如Reinmar所說,CKEditor為null / undefined。

現在,即使您在CKEditor外部單擊,我也可以找到使用當前實例的方法。

這是測試的示例文件。 http://www.megafileupload.com/en/file/448409/test-ckeditor-zip.html

我做的是......

  1. 在javascript中添加當前實例的全局變量(在html中)
  2. 在打開模態對話框的單擊按鈕上,獲取當前實例並在選項1中設置為全局變量。
  3. 在模態對話框中的iframe中單擊插入按鈕,獲取window.parent.current_instance變量並將其用作texteditor id。 var parent_cke_current_id = window.parent.current_instance_id;
  4. 現在使用單擊的CKEditor實例執行您想要的任何操作。 例如( CKEDITOR.instances[parent_cke_current_id].insertElement(element);

謝謝Reinmar。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM