将鼠标光标置于文本块的末尾 [英] Set mouse cursor at the end of the block of text

查看:152
本文介绍了将鼠标光标置于文本块的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$(id).focus(); 将光标位置设置为输入框的开头,但是我想把它放在文本,在最后一行和最后一个位置。

解决方案

试试这个:

  $(document).ready(function(){
$(#search)。focus(function(){
if .setSelectionRange)
{
var len = $(this).val()。length; $ b $ this.setSelectionRange(len,len);
}
else
{
$(this).val($(this).val());
}

});

$ (#search)。focus();
});

带输入的演示

使用textarea进行演示



因为所有浏览器都不支持 setSelectionRange 。所以最好的解决方案是将 setSelectionRange $(this).val($(this).val()); 。欲了解更多信息,请查看:使用JavaScript将光标置于文本输入元素的文本末尾


$(id).focus(); sets the cursor position to the beginning of an input box, but I'd like to place it at the end of the text, in the last row and at the last position.

解决方案

Try this:

$(document).ready(function(){
        $("#search").focus(function(){
            if (this.setSelectionRange)
            {
            var len = $(this).val().length;
            this.setSelectionRange(len, len);
            }
            else
            {
            $(this).val($(this).val());
            }

    });

    $("#search").focus();
    });

Demo with input

Demo with textarea

Because setSelectionRange is not supported by all browsers. So the best solution is to combine setSelectionRange with $(this).val($(this).val());. For more information, check out: Use JavaScript to place cursor at end of text in text input element

这篇关于将鼠标光标置于文本块的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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