Skip to content

Instantly share code, notes, and snippets.

@iHaPBoy
Created March 6, 2017 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iHaPBoy/3ed0e94ccdd3a0337b00169fcc3d113f to your computer and use it in GitHub Desktop.
Save iHaPBoy/3ed0e94ccdd3a0337b00169fcc3d113f to your computer and use it in GitHub Desktop.
JavaScript 移动光标到文本末尾
function moveEnd(obj) {
obj.focus();
var len = obj.value.length;
if (document.selection) {
var sel = obj.createTextRange();
sel.moveStart('character', len);
sel.collapse();
sel.select();
} else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
obj.selectionStart = obj.selectionEnd = len;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment