如何使用jQuery在文本字段中的字符串末尾设置光标位置? [英] How to set the cursor position at the end of a string in a text field using jQuery?

查看:48
本文介绍了如何使用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();
    }
});

这篇关于如何使用jQuery在文本字段中的字符串末尾设置光标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆