繁体   English   中英

在CKEditor中,getSelection()在IE中返回空值

[英]In CKEditor getSelection() returns null value in IE

我有一个小的代码来选择CKEditor中的文本。 为此,我在javascript中使用以下代码。

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

在Mozilla Firefox中可以正常工作。但是在IE9中,由于selectElement不是对象,因此会引发错误。 所以我检查了代码,发现getSelection()具有空值。 请帮我解决。 即使在CKEditor fourms中,我也尝试了在各种站点中给出的一些答案,但没有任何帮助。

那是正确的解决方案:

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();

我在http://ckeditor.com/demo上的Firefox,Chrome和IE8的控制台上对此进行了测试,并且可以正常工作。

这可能有效。

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();

这可能与IE9认为是对象有关。 您是否尝试过选择其他元素类型?

也许抓住元素的父元素会给您IE9认为是对象的东西,您可以尝试以下方法:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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