Add event listener to an element while inserting into CKEDITOR?

 ̄綄美尐妖づ 提交于 2019-12-13 02:27:02

问题


I'm newbie to CKEDITOR. It might sound worthless to some of you to answer this questions. But, I'm struggling to find a solution for my problem for the past few hours.

Aim :

I would like to add an event listener to particular kind of element ( For ex : span )

What i tried :

I used contentDom event thrown by CKEDITOR, to add the event listener to span elements.

Problem :

However, Adding event listener to span will be applicable for the span which are currently available in editor. But, Not for the elements ( span ) which will be created by the user in future. What should i do now?


回答1:


Use the benefits of event bubbling [1, 2]. Attach the listener to the topmost element of the editor (editable) and filter out the events:

CKEDITOR.replace( 'editor1', {
    on: {
        contentDom: function() {
            this.editable().on( 'click', function( evt ) {
                var target = evt.data.getTarget();

                if ( target.is( 'span' ) ) {
                    console.log( 'clicked span!' );
                }
            } );
        }
    }
} );


来源:https://stackoverflow.com/questions/27416060/add-event-listener-to-an-element-while-inserting-into-ckeditor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!