将光标移动到输入字段的开头? [英] move cursor to the beginning of the input field?

查看:109
本文介绍了将光标移动到输入字段的开头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您在Stackoverflow中点击提问时,您会看到一条文字您的编程问题是什么?是描述性的。

when you click on 'Ask question' here in Stackoverflow you see a text "What's your programming question? Be descriptive."

我想要同样的事情和所有我需要的是将光标移动到文本字段的开头。我如何用jquery做到这一点?

i want the same thing and all i need for that is to move my cursor to the beginning of the text field. how do i do that with jquery?

推荐答案

可能是一个矫枉过正,但这些功能有助于选择一部分输入字段。

May be that is an overkill, but these functions are helpful to select a portion of an input field.

下面的代码将光标放在第二个字段的第一个字符处。

The code below place the cursor to the first char of the second field.

<html>
<head>
    <title>so</title>
</head>
<body>
    <input value="stack" />
    <input value="overflow" />
    <script>
        var inp = document.getElementsByTagName('INPUT')[1];
        if (inp.createTextRange) {
            var part = inp.createTextRange();
            part.move("character", 0);
            part.select();
        } else if (inp.setSelectionRange) {
            inp.setSelectionRange(0, 0);
        }
        inp.focus();
    </script>
</body>
</html>

这篇关于将光标移动到输入字段的开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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