Created
October 9, 2015 17:50
-
-
Save Loiree/e2820847b7dda431f3b0 to your computer and use it in GitHub Desktop.
Позиция курсора
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doGetCaretPosition (ctrl) { | |
var CaretPos = 0; // IE Support | |
if (document.selection) { | |
ctrl.focus (); | |
var Sel = document.selection.createRange (); | |
Sel.moveStart ('character', -ctrl.value.length); | |
CaretPos = Sel.text.length; | |
} | |
// Firefox support | |
else if (ctrl.selectionStart || ctrl.selectionStart == '0') | |
CaretPos = ctrl.selectionStart; | |
return (CaretPos); | |
} | |
function setCaretPosition(ctrl, pos){ | |
if(ctrl.setSelectionRange) | |
{ | |
ctrl.focus(); | |
ctrl.setSelectionRange(pos,pos); | |
} | |
else if (ctrl.createTextRange) { | |
var range = ctrl.createTextRange(); | |
range.collapse(true); | |
range.moveEnd('character', pos); | |
range.moveStart('character', pos); | |
range.select(); | |
} | |
} | |
// где ctrl — Textarea object | |
// link — http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment