这是我使用的功能(位于我自己的自定义插件之一中)
// sets the caret position
// ed is the editor instance
// element is the html element located in the editor
// start defines if the caret should get set to the start of the element (otherwise the end)
setCursor: function (ed, element, start) {
var doc = ed.getDoc();
var edwin = ed.getWin();
if (typeof doc.createRange != "undefined") {
var range = ed.selection.dom.createRng();
range.selectNodeContents(element);
range.collapse(start);
var win = doc.defaultView || doc.parentWindow;
var sel = tinymce.isIE ? doc.selection : edwin.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof doc.body.createTextRange != "undefined") {
var textRange = doc.body.createTextRange();
textRange.moveToElementText(element);
textRange.collapse(start);
textRange.select();
}
},
示例调用:
setCursor(ed, $(ed.getBody()).find('span#caret_pos_holder').get(0), 0);