0

I've been searching a lot but i couldn't figure out how to move the text cursor in a contentEditable div,the problem is that i don't have much knowledge in javascript and the best working code i found doesn't work when the div content has multiple lines,the snippet is bellow:

function moveCaret(win, charCount) {
    var sel, range;
    if (win.getSelection) {
        sel = win.getSelection();
        if (sel.rangeCount > 0) {
            var textNode = sel.focusNode;
            var newOffset = sel.focusOffset + charCount;
            sel.collapse(textNode, Math.min(textNode.length, newOffset));
        }
    }else if ( (sel = win.document.selection) ) {
        if (sel.type != "Control") {
            range = sel.createRange();
            range.move("character", charCount);
            range.select();
        }
    }
}

So basically i need a javascript function to move the caret in a contentEditable div,assuming that the div may have multiple lines.

Any advices,link or source code will be appreciate.Thanks in advance!

4

0 回答 0