【问题标题】:HTML Textarea: is it possible to change cursor position?HTML Textarea:是否可以更改光标位置?
【发布时间】:2013-04-26 19:22:01
【问题描述】:

是否可以使用 Javascript 代码更改 textarea 元素中的光标位置?

【问题讨论】:

标签: javascript html


【解决方案1】:

是的

function SetCursorPosition(pos)
{
    // HERE txt is the text field name
    var obj=document.getElementById('<%= txt.ClientID %><%= txt.ClientID %>');

    //FOR IE
    if(obj.setSelectionRange)
    {
        obj.focus();
        obj.setSelectionRange(pos,pos);
    }

    // For Firefox
    else if (obj.createTextRange)
    {
        var range = obj.createTextRange();
        range.collapse(true);
        range.moveEnd('character', pos);
        range.moveStart('character', pos);
        range.select();
    }
}

FROM :: http://shawpnendu.blogspot.com/2009/03/javascript-how-to-setget-cursor.html

【讨论】:

    【解决方案2】:

    这篇文章可能对你有所帮助Caret position in textarea, in characters from the start

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 2011-05-04
    相关资源
    最近更新 更多