使用這個話題: jQuery Set Cursor Position in Text Area輸入的文本中設置光標使用jQuery
我寫這篇文章的代碼,但它不工作:
<input id="myTextInput" type="text" value="some text2">
<input type="button" value="set mouse" id="btn" />
和:
$(document).ready(function() {
$('#btn').on('click', function() {
var inp = $('#myTextInput');
var pos = 3;
inp.focus();
if (inp.setSelectionRange) {
inp.setSelectionRange(pos, pos);
} else if (inp.createTextRange) {
var range = inp.createTextRange();
range.collapse(true);
if (pos < 0) {
pos = $(this).val().length + pos;
}
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
});
});
我的錯誤在哪裏? 謝謝
正確的搜索詞是'caret' – 2014-11-06 07:29:38