简体   繁体   中英

In CKEditor getSelection() returns null value in IE

I am having an small code to select an text in CKEditor. For that i am using following code in javascript.

        var docx = editor.document;
        var elementx = docx.getById(id);
        editor.getSelection().selectElement(elementx);
        editor.getSelection().scrollIntoView(true);

It works fine in Mozilla Firefox.But in IE9 it throws an error as selectElement is not an object. So i checked the code and found that getSelection() having an null value. Please help me how to solve it. I tried some answers given in various sites even in CKEditor fourms nothing helped me.

That's the correct solution:

var editor = CKEDITOR.instances.editor1;
editor.focus(); // Without this selection will be null on IE.

var element = editor.document.getBody().getLast(),
    selection = editor.getSelection();

selection.selectElement(element); // You have to reuse selection.
selection.scrollIntoView();

I tested this from the console on Firefox, Chrome and IE8 on http://ckeditor.com/demo and it worked.

This might work.

var docx = editor.document;
var elementx = docx.getById(id);

var resRange = new CKEDITOR.dom.range( editor.document );
resRange.selectNodeContents( elementx );
resRange.collapse();
editor.getSelection().selectRanges( [ resRange ] );
resRange.endContainer.$.scrollIntoView();

This may have something to do with what IE9 considers to be an object. Have you tried selecting different element types?

Maybe grabbing the parent of the element will give you something that IE9 considers to be an object, you can try this:

var docx = editor.document;
var elementx = docx.getById(id);
var parentx = elementx.getParent();
parentx.scrollIntoView();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM