javascript - 如何使用 jQuery 将光标位置设置在文本字段中字符串的末尾?

标签 javascript jquery focus cursor textfield

我正在使用 jQuery 1.6,我有一个文本字段,我希望在该字段获得焦点后将光标定位在字符串\文本的末尾。是否有简单或容易做到这一点?

此时我使用的是如下代码:

$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();
    }
});

关于javascript - 如何使用 jQuery 将光标位置设置在文本字段中字符串的末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7368750/

相关文章:

javascript - iframe 高度 chop

javascript - 在 jQuery 中每 5 秒调用一次函数的最简单方法是什么?

javascript - jQuery Accordion - 将选定的一个向上移动

jquery - 获取 405 method not allowed 异常

jquery - MVC 主详细信息保存错误

java - setText() 后的 JEditorPane 焦点问题

android - 在 Horizo​​ntal ScrollView 中滚动会失去焦点元素的焦点,

HTML5 : How to set focus on a text input in a list with AngularJS

javascript - Chrome 跨域 PATCH 请求不起作用

javascript - 将枚举对象转换为数组在 javascript 中返回额外的对象