Created
August 27, 2014 01:18
-
-
Save ziluo/a1673ea5e3fc555cc250 to your computer and use it in GitHub Desktop.
让光标定位于input的内容的最后
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 setCursor(input) { | |
input.focus() | |
if (window.netscape) { | |
setTimeout(function() { //方法一 W3C,不过除firefox的浏览器可以直接用input.value = input.value搞定 | |
var n = input.value.length | |
input.setSelectionRange(n, n); | |
input.focus() | |
}, 0) | |
var n = input.value.length //方法二 | |
input.selectionStart = n | |
input.selectionEnd = n | |
} else { | |
input.value = input.value // 方法三 让光标位于文字之的后 | |
if (input.createTextRange) { //方法四 IE only | |
var range = input.createTextRange(); //建立文本选区 | |
range.moveStart('character', input.value.length); //选区的起点移到最后去 | |
range.collapse(true); | |
range.select(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment