<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<meta name="author" content="http://weibo.com/zswang" />
<title>Demo 示例控制输入光标位置</title>
<style> </style>
</head>
<body>
<input id="editor" type="text" value="1234" />
<input id="left" type="button" value="left" >
<input id="right" type="button" value="right" >
<script>
void function(){
function setSelection(editor, pos){
if (editor.setSelectionRange){
editor.focus();
editor.setSelectionRange(pos, pos);
} else if (editor.createTextRange){
var textRange = editor.createTextRange();
textRange.collapse(true);
textRange.moveEnd("character", pos);
textRange.moveStart("character", pos);
textRange.select();
}
} var editor = document.getElementById('editor');
document.getElementById('left').onclick = function(){
setSelection(editor, 0);
}
document.getElementById('right').onclick = function(){
setSelection(editor, editor.value.length);
}
}();
</script>
</body>
</html>
$("#db_name").bind("mousemove keyup",function(){
var editor=document.getElementById('db_name');
var val=editor.value;
setSelection(editor, val.length); //光标控制在右边
//setSelection(editor, 0); //光标控制在左边
})
资料来源:http://bbs.csdn.net/topics/380246235#post-390813797