问题
I am using CKEditor 4.4.3 and trying to listen to an editor's click event:
editor.on('click', function (e) {
console.log('click event from attaching to the editor');
});
For some reason, the click event never fires. However, if I listen to the doubleclick
event, it fires when the editor is double clicked.
I was previously listening to the click event on editor.editable
, but it doesn't appear to work for editors that are not inlined. Why is the click
event not working?
Some further investigations:
Attaching the event handler on editor.document
fires for every click, including clicks outside the editor.
Attaching the event handler to editor.container
fires for clicks on the container including the toolbars.
Fiddle: http://jsfiddle.net/295PE/
回答1:
Correct way of attaching listeners to the editable element in CKEditor:
editor.on( 'contentDom', function() {
var editable = editor.editable();
editable.attachListener( editable, 'click', function() {
// ...
} );
} );
Read more about the reasons why contentDom
event has to be used instead of e.g. instanceReady in editable.attachListener documentation.
回答2:
Use attach the event handler to the editor's editable
. This needs to be done after the editor is ready:
editor.on('instanceReady', function (e) {
editor.editable().on('click', function (event) {
console.log('clicked');
});
});
Fiddle: http://jsfiddle.net/8fZpz/
来源:https://stackoverflow.com/questions/24989766/ckeditors-click-event-not-firing