一尘不染

获取HTML输入中的插入符位置?

javascript

如何获取输入中文本插入符号的索引?


阅读 297

收藏
2020-05-01

共1个答案

一尘不染

-> 选择开始

<!doctype html>

<html>
    <head>
        <meta charset = "utf-8">

        <script type = "text/javascript">
            window.addEventListener ("load", function () {
                var input = document.getElementsByTagName ("input");

                input[0].addEventListener ("keydown", function () {
                    alert ("Caret position: " + this.selectionStart);

                    // You can also set the caret: this.selectionStart = 2;
                });
            });
        </script>

        <title>Test</title>
    </head>

    <body>
        <input type = "text">
    </body>
</html>
2020-05-01