DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> js獲取光標位置和設置文本框光標位置示例代碼
js獲取光標位置和設置文本框光標位置示例代碼
編輯:關於JavaScript     

復制代碼 代碼如下:
<script type="text/javascript">
        function getTxt1CursorPosition(){
            var oTxt1 = document.getElementById("txt1");
            var cursurPosition=-1;
            if(oTxt1.selectionStart){//非IE浏覽器
                cursurPosition= oTxt1.selectionStart;
            }else{//IE
                var range = document.selection.createRange();
                range.moveStart("character",-oTxt1.value.length);
                cursurPosition=range.text.length;
            }
            alert(cursurPosition);
        }

        function setTxt1CursorPosition(i){
            var oTxt1 = document.getElementById("txt2");
            var cursurPosition=-1;
            if(oTxt1.selectionStart){//非IE浏覽器
                oTxt1.selectionStart=i;
            }else{//IE
                var range = oTxt1.createTextRange();
                range.move("character",i);
                range.select();
            }
        }

        function getTa1CursorPosition(){
            var evt =window.event?window.event:getTa1CursorPosition.caller.arguments[0];
            var oTa1 = document.getElementById("ta1");
            var cursurPosition=-1;
            if(oTa1.selectionStart){//非IE浏覽器
                cursurPosition= oTa1.selectionStart;
            }else{//IE
                var range = oTa1.createTextRange();
                range.moveToPoint(evt.x,evt.y);
                range.moveStart("character",-oTa1.value.length);
                cursurPosition=range.text.length;
            }
            alert(cursurPosition);
        }

        function setTa1CursorPosition(i){
            var oTa2 = document.getElementById("ta2");
            if(oTa2.selectionStart){//非IE浏覽器
                oTa2.selectionStart=i;
                oTa2.selectionEnd=i;
            }else{//IE
                var range = oTa2.createTextRange();
                range.move("character",i);
                range.select();
            }
        }
    </script>

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved