CKEDITOR.setData阻止事件附加.on函数 [英] CKEDITOR.setData prevents attaching of events with .on function
问题描述
我已经构建了一些自定义插件,但只有一个正在收听键盘的关键事件。
下面的代码中,您可以看到设置事件的设置。 (和它有点基本)
现在我有以下问题,如果我设置我的数据与editor.setData在一个instanceReady监听器中.on函数不是
我确实尝试用instanceReady事件替换contentDom,但是也不能修复它。
如果我将数据手动设置为:editor.document.getBody()。setHtml(html),没有问题。所有的事件都没有任何问题。
CKEDITOR.plugins.add('myPlugin',{
lang :'',//%REMOVE_LINE_CORE%
init:function(editor){
//如果Dom准备好,绑定事件
editor.on 'contentDom',function()
{
// keydown
editor.document.on('keydown',function(e)
{
有没有人知道为什么会发生这种情况?setData函数只设置html还是重新加载编辑器? >
我看了这个 Ckeditor Source
但我认为这不是与setData函数有关的代码。
我不想要一个解决方案,我喜欢理解为什么会发生这种情况。
编辑#contentDom $每次新的内部文档设置都会触发c $ c>
。在框架编辑器中编辑器#setData()
不仅替换 body.innerHTML
而是整个文档,所以 contentDom
每次都被触发。
因此,你的代码在每个 setData()
但你不删除旧的。由于不清楚的原因,这两个听众中没有一个现在在 keydown
上被触发。我最近发现了这一点,我无法解释这个事实。
无论如何,你需要在 编辑#contentDomUnload
。幸运的是,使用 可编辑#attachListener
。
editor.on('contentDom',function(){
var editable = editor.editable();
editable.attachListener(editor.document,'keydown',function(){
...
});
});
听众将自动分离在下一个 contentDomUnload
。
I have build a few custom plugins, but only one is listening to key events of the keyboard. Below in the code you can see the set-up to set the events. (and it's kinda basic)
Now i have the following problem that if i set my data with editor.setData in a instanceReady listener.that the .on functions aren't set.
I did try to replace the contentDom with the instanceReady event, but that doesn't fix it either.
if i set the data manualy with: editor.document.getBody().setHtml(html), there are no problems. and all events are attached without any problems..
CKEDITOR.plugins.add( 'myPlugin', {
lang: '', // %REMOVE_LINE_CORE%
init: function( editor ) {
//Bind events if the Dom is ready!
editor.on( 'contentDom', function()
{
//keydown
editor.document.on('keydown', function(e)
{
Does anyone know why this is happening? Does the setData function only set the html or does it also reload the editor or something?
I did take a look at this Ckeditor Source But i think that this isn't code that has something to do with the setData function.
I'm not asking for a solution. I like to understand why it's happening.
Editor#contentDom
is fired every time new inner document is set up. In framed editor editor#setData()
replaces not only body.innerHTML
but entire document, so contentDom
is fired every time.
Thus, your code adds new listener on every setData()
but you don't remove the old one. For unclear reasons none of these two listeners are now fired on keydown
. I found out this recently and I cannot explain this fact.
Anyway, you need to detach all listeners on editor#contentDomUnload
. Fortunately, there's convenient way to do that by using editable#attachListener
.
editor.on( 'contentDom', function() {
var editable = editor.editable();
editable.attachListener( editor.document, 'keydown', function() {
...
} );
} );
Listener will be automatically detached on next contentDomUnload
.
这篇关于CKEDITOR.setData阻止事件附加.on函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!