CkEditor - 未捕获TypeError:无法读取未定义的属性'getSelection'

时间:2017-02-02 14:57:28

标签: javascript jquery ckeditor

我正在我的应用中实施CKEditor。当我试图将CKEditor实例化到textarea时,我收到了错误

Cannot read property 'getSelection' of undefined

在ckeditor的下面一行

getNative: function() {
 return void 0 !== this._.cache.nativeSel ? this._.cache.nativeSel : this._.cache.nativeSel = B ? this.document.$.selection : this.document.getWindow().$.getSelection() }

非常感谢任何帮助。

5 个答案:

答案 0 :(得分:2)

  1. 我有一份文章清单。
  2. 每次我点击任何一篇"对话框/模态"应该是开放的。
  3. 在这样的对话框或模态中,我的文章内容中有一个ckeditor元素。
  4. 当我点击它时,它就像一个魅力。
  5. 问题出在点击第2,第3,第4等之后。
  6. 然后我开始出现这个错误。

        TypeError: Cannot read property 'getSelection' of undefined
        at CKEDITOR.dom.selection.getNative (ckeditor.js:448)
        at new CKEDITOR.dom.selection (ckeditor.js:446)
        at a.CKEDITOR.editor.getSelection (ckeditor.js:443)
        at new CKEDITOR.plugins.undo.Image (ckeditor.js:1182)
        at CKEDITOR.plugins.undo.UndoManager.save (ckeditor.js:1177)
        at a.<anonymous> (ckeditor.js:1173)
        at a.n (ckeditor.js:10)
        at a.CKEDITOR.event.CKEDITOR.event.fire (ckeditor.js:12)
        at a.CKEDITOR.editor.CKEDITOR.editor.fire (ckeditor.js:13)
        at a.setData (ckeditor.js:275)
    

    对我来说解决方案很简单,告诉计算机在关闭对话框/模态时销毁ckeditor实例。简单!..现在像魅力一样工作=)

               $mdDialog.show({
                 parent:      parentEl,
                 targetEvent: $event,
                 templateUrl: '/md-templates/blog-article.html',
                 controller:  DialogController,
                 scope:         $scope,
                 preserveScope: true,
                onRemoving: function (event, removePromise) {
                    if (CKEDITOR.instances.body) CKEDITOR.instances.body.destroy();
                }
            });
    

答案 1 :(得分:0)

我遇到了同样的错误,我通过初始化CKEditor解决了 $(document).function(ready());

$(document).ready(function () {
    CKEDITOR.replace('editor1', {
        language: 'tr',
        height: '300'
    });
});

我认为在页面加载之前进行初始化时,找不到dom元素(文本区域)

答案 2 :(得分:0)

您可以尝试

CKEditor.destroy();

答案 3 :(得分:0)

这可能是您的应用程序在准备好编辑器之前尝试访问并设置CKEditor的数据。这可能是争用条件的结果,导致错误是间歇性的。您可以采取一些措施来防止此问题。

首先,使用CKEditor endorsed方法测试编辑器是否首先loaded

if ( CKEDITOR.status == 'loaded' ) {
    // The API can now be fully used.
    doSomething();
} else {
    // Wait for the full core to be loaded and fire its loading.
    CKEDITOR.on( 'load', doSomething );
    CKEDITOR.loadFullCore && CKEDITOR.loadFullCore();
}

第二,如果您无法控制与设置编辑器值相关的时间,则可以将CKEDITOR.dom.window对象包装在async / await Promise中测试是否首先加载编辑器,如果未加载,则测试是否要加载编辑器,然后完成设置值。 (请注意,此代码尚未经过全面测试)

CKEDITOR.dom.window = (function(window) {
    if ( CKEDITOR.status == 'loaded' ) {
        // The API can now be fully used.
        return window;
    } else {
        return (async function() {
            return await function() {
                return new Promise(resolve => {
                    CKEDITOR.on( 'load', () => {
                        resolve(window);
                    });
                });
            };
        })();

        CKEDITOR.loadFullCore && CKEDITOR.loadFullCore();
    }
})(CKEDITOR.dom.window);

答案 4 :(得分:-4)

将ckeditor.js中的函数f。$。onload更改为下面的

 f.$.onload=function(){var toutMs =5000;
    if(CKEDITOR.document.getHead().$.childElementCount > 6)
    {
    toutMs=0;
      }
    setTimeout(function(){
    A(b,!0)
     },toutMs)
     }