CKEditor: set cursor/caret positon

好久不见. 提交于 2019-12-02 02:26:13

问题


How can I position the caret in CKEditor3.x? I have 2 positions and I want use insertHTML() on both positions.

Pseudo-code:

editor.setCaret(20); // function does not exists
editor.insertHtml('::');
editor.setCaret(40); // function does not exists
editor.insertHtml('::');

I have tried (to set caret to position: 20):

var ranges = [];
var range = new CKEDITOR.dom.range( this.document );
range.startOffset = 20;
range.endOffset = 20;
ranges.push( range );
editor.getSelection().selectRanges( ranges );

This is not working. Can anybody help me please?


回答1:


To insert text or do something with html in the editor you don't need to get the caret position.

Treat it as usual html.

P.S. If you probably want to restore cursor position after dom manipulating, try this

var s = editor.getSelection();
var selected_ranges = s.getRanges(); // save selected range
// do something
s.selectRanges(selected_ranges); // restore it



回答2:


If you use insertElement instead of insert html (and say, insert a span element) the following should probably work:

editor.insertElement(element);
var range = new CKEDITOR.dom.range(editor.document);
range.moveToElementEditablePosition(element, true);
editor.getSelection().selectRanges([range]);


来源:https://stackoverflow.com/questions/5742599/ckeditor-set-cursor-caret-positon

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!