zoukankan      html  css  js  c++  java
  • Demo 示例控制输入光标位置

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset=utf-8 />
        <meta name="author" content="http://weibo.com/zswang" />
        <title>Demo 示例控制输入光标位置</title>
        <style>
             
        </style>
    </head>
    <body>
        <input id="editor" type="text" value="1234" />
        <input id="left" type="button" value="left" >
        <input id="right" type="button" value="right" >
        <script>
    void function(){
        function setSelection(editor, pos){
            if (editor.setSelectionRange){
                editor.focus();
                editor.setSelectionRange(pos, pos);
            } else if (editor.createTextRange){
                var textRange = editor.createTextRange();
                textRange.collapse(true);
                textRange.moveEnd("character", pos);
                textRange.moveStart("character", pos);
                textRange.select();
            }
        }
       
        var editor = document.getElementById('editor');
        document.getElementById('left').onclick = function(){
            setSelection(editor, 0);
        }
        document.getElementById('right').onclick = function(){
            setSelection(editor, editor.value.length);
        }
    }();
        </script>
    </body>
    </html>
    $("#db_name").bind("mousemove keyup",function(){
    var editor=document.getElementById('db_name');
    var val=editor.value;
    setSelection(editor, val.length); //光标控制在右边
    //setSelection(editor, 0); //光标控制在左边
    })

    资料来源:http://bbs.csdn.net/topics/380246235#post-390813797

  • 相关阅读:
    打印日志宏定义
    数据库读写操作
    SQL语句组成
    MySQL数据库的使用
    ubuntu下解决MySQL 1045 error
    css样式优先级
    redis
    dubbo
    maven
    Mybatis笔记
  • 原文地址:https://www.cnblogs.com/cyun/p/5581147.html
Copyright © 2011-2022 走看看