如何将光标放在textarea的开头 [英] Howto Place cursor at beginning of textarea

查看:132
本文介绍了如何将光标放在textarea的开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一些关于如何将光标放在文本末尾的textarea中的资源,但是我无法找到一种简单的方法让它出现在开头。

I've found a couple resources for how to place the cursor in a textarea at the end of the text, but I can't sort out a simple way to make it appear at the beginning.

我正在使用一些文本预填充textarea,只是想让用户更容易。

I'm prepopulating the textarea with some text and just want to make it easier on the users.

推荐答案

将对textarea的引用传递给此JS函数。

Pass a reference to your textarea to this JS function.

function resetCursor(txtElement) { 
    if (txtElement.setSelectionRange) { 
        txtElement.focus(); 
        txtElement.setSelectionRange(0, 0); 
    } else if (txtElement.createTextRange) { 
        var range = txtElement.createTextRange();  
        range.moveStart('character', 0); 
        range.select(); 
    } 
}

这篇关于如何将光标放在textarea的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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