在textarea中需要光标在文本的开头 [英] Need cursor at beginning of text in textarea

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

问题描述

我有这个在我的身体,它的工作原理

I have this in my body and it works

onLoad='document.forms.post.message.focus()'

但是我需要将光标放在任何现有文本开头的textarea中,而不是在末尾。这就把它放在最后。

but I need the cursor to be placed in the textarea at the beginning of any existing text, not at the end. This puts it at the end.

请注意,我对JavaScript一无所知,所以请保持温和。

Note that I know nothing about JavaScript, so be gentle.

谢谢

推荐答案

function moveCaretToStart(el) {
    if (typeof el.selectionStart == "number") {
        el.selectionStart = el.selectionEnd = 0;
    } else if (typeof el.createTextRange != "undefined") {
        el.focus();
        var range = el.createTextRange();
        range.collapse(true);
        range.select();
    }
}

moveCaretToStart(document.forms["post"].elements["message"]);

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

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