在HTML输入中获取插入位置? [英] Get caret position in HTML input?

查看:104
本文介绍了在HTML输入中获取插入位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在输入中获得文本插入符号的索引? 解决方案

- > selectionStart

 <!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 ,函数(){
alert(插入位置:+ this.selectionStart);

//你也可以设置插入符号:this.selectionStart = 2;
} );
});
< / script>

< title>测试< / title>
< / head>

< body>
< input type =text>
< / body>
< / html>


How do I get the index of the text caret in an input?

解决方案

-> selectionStart

<!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>

这篇关于在HTML输入中获取插入位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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