Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否可以将光标锁定/聚焦到文本区域的最后一行?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 是否可以将光标锁定/聚焦到文本区域的最后一行?

Javascript 是否可以将光标锁定/聚焦到文本区域的最后一行?,javascript,jquery,html,Javascript,Jquery,Html,假设我们的html代码中有一个textarea。 此textarea包含一些 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Cursor Test</title> </head> <body> <textarea class="text" cols="200" rows="40"> Some Random text. More

假设我们的html代码中有一个
textarea

textarea
包含一些

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Cursor Test</title>
</head>
<body>
    <textarea class="text" cols="200" rows="40">
Some Random text.
More random text
> [fixed cursor position]
    </textarea>
</body>
</html>

光标测试
一些随机文本。
更多随机文本
>[固定光标位置]
是否可以防止光标在指定位置上方或之前移动?(在标记为
[固定光标位置]
的代码中)
我希望仍然能够写作,但我不想超过起始位置。(起始位置上方或之前->
[固定光标位置]

因此,如果焦点在
textarea
上,则在键入时光标应移动到其位置。(按ON键,仍能在
文本区域中选择和标记文本

在这种情况下->(至少)第3行第3列


这可以在JavaScript或JQuery中完成吗

有一个小功能可以帮助您:

jQuery.fn.lockCursor = function() {

    return this.each(function() { //return the function for each object

        if (this.setSelectionRange) { //check if function is available

            var len = $(this).val().length * 2; //avoid problems with carriage returns in text
            this.setSelectionRange(len, len);

        } else {

            $(this).val($(this).val()); //replace content with itself
            //Will automatically set the cursor to the end

       }

    });

};
$('.text').keypress(function(){
    $(this).lockCursor(); //execute function on keypress
});

编辑

我更新了我的代码,以便编辑我自己添加的文本:

var selectEnd; //global variable
jQuery.fn.lockCursor = function() {
    return this.each(function() {
        var len = $(this).val().length * 2;
        this.setSelectionRange(len, len);        
    });
};

$('.text').focus(function(){
    $(this).lockCursor();
    selectEnd = $(this).val().length; //save original length of value
});

$('.text').keypress(function(){
    if($(this)[0].selectionStart < selectEnd){ //cursor position is lower then initial position
        $(this).lockCursor();
    }
});
var-selectEnd//全局变量
jQuery.fn.lockCursor=函数(){
返回此值。每个(函数(){
var len=$(this.val().length*2;
此.设置选择范围(len,len);
});
};
$('.text').focus(函数(){
$(this.lockCursor();
selectEnd=$(this).val().length;//保存值的原始长度
});
$('.text').keypress(函数(){
如果($(this)[0].selectionStart
说明:

$(this)[0]。selectionStart
是必需的,因为它只能是一个元素,而不是jQuery对象提供的一组元素

注意:此解决方案可能与所有浏览器和版本不兼容,具体取决于它们对
selectionStart
-方法的支持

参考


将光标的最低可能位置保存在js var中,然后在文本区域启用FOCUS和onkeydown,检查光标的位置,如果光标位于<最低可能位置,则将光标设置为最低可能位置它工作正常,但有一个问题。如果你键入文本,你不能在中间编辑它。它总是转到edittable文本的最后一个位置。因此,如果您键入“helo”,您必须删除“o”以添加缺少的“l”。