zoukankan      html  css  js  c++  java
  • HTML Input Text cursor position control

    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title> new document </title>
        <META NAME="Generator" CONTENT="EditPlus,Microshaoft">
        <META NAME="Author" CONTENT="EditPlus,Microshaoft">
        <META NAME="Keywords" CONTENT="EditPlus,Microshaoft">
        <META NAME="Description" CONTENT="EditPlus,Microshaoft">
        <script type="text/javascript">
        <!--
            function GetTextBoxCursorPosition(inputText) {
                var cursorPosition = 0;
                // IE
                if(document.selection) {
                    inputText.focus();
                    var textRange = document.selection.createRange();
                    textRange.moveStart('character', - inputText.value.length);
                    cursorPosition = textRange.text.length;
                }
                // Firefox
                else if(inputText.selectionStart || inputText.selectionStart == '0') {
                    cursorPosition = inputText.selectionStart;
                }
                return cursorPosition;
            }
            function SetInputTextCursorPosition(inputText, cursorPosition) {
                // IE
                if(document.selection) { 
                    inputText.focus();
                    var textRange = document.selection.createRange();
                    textRange.moveStart('character', - inputText.value.length);
                    textRange.moveStart('character', cursorPosition);
                    textRange.moveEnd('character', 0);
                    textRange.select();
                }
                else if(inputText.selectionStart || inputText.selectionStart == '0') {
                    inputText.selectionStart = cursorPosition;
                    inputText.selectionEnd = cursorPosition;
                    inputText.focus();
                }
            }
        //-->
        </script>
    </head>
    <body>
        <table border="1">
            <tr>
                <td colspan="2" align="center">
                    <input type="text" id="text1" value="!@#123QWEasd" />
                </td>
            </tr>
            <tr>
                <td>
                    <input type="button" id="button1" value="Set Cursor Position" onclick="SetInputTextCursorPosition(document.getElementById('text1'), parseInt(document.getElementById('text2').value));" />
                </td>
                <td>
                    <input type="text" id="text2" />
                </td>
            </tr>
        </table>
        <script type="text/javascript">
        <!--
            var x = document.getElementById("text1");
            x.onkeydown = x.onclick = x.onselect = onCursorPositionChange;
            function onCursorPositionChange()
            {
                var sender = window.event.srcElement;
                if (sender.type == "text")
                {
                    document.getElementById("text2").value = GetTextBoxCursorPosition(sender);
                }
            }
        //-->
        </script>
    </body>
    </html>
    
    
  • 相关阅读:
    滚动条滑至底部自动加载内容
    curl请求https请求
    JS根据经纬度获取地址信息
    php结合md5的加密解密算法实例
    php gzcompress() 和gzuncompress()函数实现字符串压缩
    html视频播放器的代码 及其参数详解
    phpcms 整合 discuz!
    phpcms V9 整合 Discuz! X2 教程
    中国各省打架排行榜
    jQuery获取输入框并设置焦点
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/2691525.html
Copyright © 2011-2022 走看看