且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

CKEditor:设置光标/插入符号

更新时间:2023-02-01 19:03:38

要在编辑器中插入文本或使用html做某事,



PS如果你可能想要的话,在dom操作后恢复光标位置,尝试此

  var s = editor.getSelection(); 
var selected_ranges = s.getRanges(); //保存所选范围
//做某事
s.selectRanges(selected_ranges); //恢复它


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

fictive code:

editor.setCaret(20); // function not exists
editor.insertHtml('::');
editor.setCaret(40); // function 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?

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