CKEditor:设置光标/插入符号位置
如何在CKEditor3.x中定位插入符号? 我有两个位置,我想在两个位置上都使用insertHTML() 伪代码:CKEditor:设置光标/插入符号位置,ckeditor,caret,Ckeditor,Caret,如何在CKEditor3.x中定位插入符号? 我有两个位置,我想在两个位置上都使用insertHTML() 伪代码: editor.setCaret(20); // function does not exists editor.insertHtml('::'); editor.setCaret(40); // function does not exists editor.insertHtml('::'); 我已尝试(将插入符号设置为位置:20): 这是行不通的。有人能帮我吗?要在编辑器中插入
editor.setCaret(20); // function does not exists
editor.insertHtml('::');
editor.setCaret(40); // function does not exists
editor.insertHtml('::');
我已尝试(将插入符号设置为位置:20):
这是行不通的。有人能帮我吗?要在编辑器中插入文本或使用html执行某些操作,您不需要获得插入符号位置 像对待html一样对待它 另外,如果您可能希望在dom操作后恢复光标位置,请尝试以下操作
var s = editor.getSelection();
var selected_ranges = s.getRanges(); // save selected range
// do something
s.selectRanges(selected_ranges); // restore it
如果使用insertElement而不是insert html(比如说,插入一个span元素),那么以下可能会起作用:
editor.insertElement(element);
var range = new CKEDITOR.dom.range(editor.document);
range.moveToElementEditablePosition(element, true);
editor.getSelection().selectRanges([range]);
如果在执行插入之前保存范围,并希望光标位于插入的元素之后,则此操作无效。它将光标放在插入的元素之前。仍然没有找到如何将光标放在插入的元素之后。正常行为是光标位于插入的元素内,这在某些用例(我的!)中不起作用。我看到有一个名为
getSelectedElement()
(请参阅)的函数,它应该返回CKEDITOR.dom.element
的实例,该实例具有方法focusNext
(请参阅)这将把焦点转移到下一个元素。但我就是不能让它工作/也许有人可以朝那个方向挖掘。。
editor.insertElement(element);
var range = new CKEDITOR.dom.range(editor.document);
range.moveToElementEditablePosition(element, true);
editor.getSelection().selectRanges([range]);