如何在 CKEditor iframe 上添加 onClick 事件

How to add onClick event on CKEditor iframe

我正在使用 CKEditor,我想在 CKEditor iframe window 的 onClick 事件上执行一些任务。即CKEditor的可编辑区域。

 CKEDITOR.instances['ckEditor'].on('contentDom', function() {
        this.document.on('click', function(event){
             // calling some other function or some logic
         });
    });

但是,上面的代码只在我第一次点击 CKEditor iframe 时有效。为什么这样?在我点击 html 页面的其他元素然后我点击 CKEditor iframe 后,它不再工作了。

请尝试在一些默认样本上使用以下代码。基本上,如果您已将 click 事件附加到编辑器 editable 并且您正在使用 contentDom 事件,那么 click 的事件处理程序应该被触发,即使在切换到源代码模式并返回实际位置之后旧的 iframe 被销毁并创建了全新的 iframe。

var editor = CKEDITOR.replace( 'editor1', {});
editor.on( 'pluginsLoaded', function( event ) {
    editor.on( 'contentDom', function( evt ) {
        editor.editable().attachListener( editor.editable(), 'click', function( e ){
            console.log( 'click' );
        } );
    } );
} );