2017-04-25 90 views
0

我查看了許多線程,但無法找到解決我問題的答案。所以,我定義了一個CKEditor的情況下,像這樣:無法捕捉到CKEditor更改事件

var editor = $('#test-editor'); 

editor.ckeditor(function() {}, { 
    customConfig: '../../assets/js/custom/ckeditor_config.js', 
    allowedContent: true 
}); 

但是我不知道我怎麼能趕上change事件。這是我試過的:

var t = editor.ckeditor(function() { 
    this.on('change', function() { 
    console.log("test 1"); 
    }); 
}, { 
    customConfig: '../../assets/js/custom/ckeditor_config.js', 
    allowedContent: true 
}); 

editor.on('change', function() { 
    console.log("test 2"); 
}); 

t.on('change', function() { 
    console.log("test 3"); 
}); 

所有這三次嘗試都以失敗告終。我應該補充一點,我不想循環瀏覽一個頁面上的所有編輯器,我只想解決一個編輯器在#test-editor處渲染的特定編輯器。我怎樣才能做到這一點?

+0

BTW,我看到我的編輯變量包含一個承諾對象。但我從來沒有與他們交涉,我不知道我怎麼能用這個事實來趕上事件, – Jacobian

回答

1

jQuery ckeditor()方法返回一個jQuery對象,該對象僅在其event handling中公開CKEditor的5個事件。爲了使用其他事件,您需要通過訪問jQuery對象的editor屬性來使用CKEditor.editor對象。

所以,你需要使用這樣的事情:

var myeditor = $('#test-editor').ckeditor({ 
    customConfig: '../../assets/js/custom/ckeditor_config.js', 
    allowedContent: true 
}); 
myeditor.editor.on('change', function(evt) { 
    console.log('change detected'); 
}); 
+0

謝謝!順便說一下,你可能還知道關於CKEditor的更多信息。對我來說,它看起來像格式,字體和大小組合框佔據了太多的地方在工具欄上。你知道嗎,如果有可能把它們改成圖標或一些自定義的字母? – Jacobian

+0

我曾經爲了樂趣而改變了他們的標籤,但是寬度保持不變(可能是因爲他們的物品的寬度)。 – Wizard