如何使用jQuery在文本字段中的字符串末尾设置光标位置?
问题描述:
我使用的是jQuery 1.6,我有一个文本字段,在该字段获得焦点之后,我希望光标位于字符串\ text的末尾.是否有琐碎或容易做到的事?
I am using jQuery 1.6 and I have a text field for which I would like the cursor to be positioned at the end of the string\text after the field receives focus. Is there a trivial or easy to do this?
这时我正在使用以下代码:
At this time I am using the following code:
$jQ('#css_id_value').focus(function(){
this.select();
});
$jQ('css_id_value').focus();
答
$('#foo').focus(function() {
if (this.setSelectionRange) {
this.setSelectionRange(this.value.length, this.value.length);
} else if (this.createTextRange) {
// IE
var range = this.createTextRange();
range.collapse(true);
range.moveStart('character', this.value.length);
range.moveEnd('character', this.value.length);
range.select();
}
});