js 커서 위치 가져오기 및 텍스트 상자 커서 위치 설정 예시 코드

2540 단어
 
  
<br>        function getTxt1CursorPosition(){ <br>            var oTxt1 = document.getElementById("txt1"); <br>            var cursurPosition=-1; <br>            if(oTxt1.selectionStart){// IE <br>                cursurPosition= oTxt1.selectionStart; <br>            }else{//IE <br>                var range = document.selection.createRange(); <br>                range.moveStart("character",-oTxt1.value.length); <br>                cursurPosition=range.text.length; <br>            } <br>            alert(cursurPosition); <br>        } <p></p> <p>        function setTxt1CursorPosition(i){<br>            var oTxt1 = document.getElementById("txt2");<br>            var cursurPosition=-1;<br>            if(oTxt1.selectionStart){// IE <br>                oTxt1.selectionStart=i;<br>            }else{//IE<br>                var range = oTxt1.createTextRange();<br>                range.move("character",i);<br>                range.select();<br>            }<br>        }</p> <p>        function getTa1CursorPosition(){<br>            var evt =window.event?window.event:getTa1CursorPosition.caller.arguments[0];<br>            var oTa1 = document.getElementById("ta1");<br>            var cursurPosition=-1;<br>            if(oTa1.selectionStart){// IE <br>                cursurPosition= oTa1.selectionStart;<br>            }else{//IE<br>                var range = oTa1.createTextRange();<br>                range.moveToPoint(evt.x,evt.y);<br>                range.moveStart("character",-oTa1.value.length);<br>                cursurPosition=range.text.length;<br>            }<br>            alert(cursurPosition);<br>        }</p> <p>        function setTa1CursorPosition(i){<br>            var oTa2 = document.getElementById("ta2");<br>            if(oTa2.selectionStart){// IE <br>                oTa2.selectionStart=i;<br>                oTa2.selectionEnd=i;<br>            }else{//IE<br>                var range = oTa2.createTextRange();<br>                range.move("character",i);<br>                range.select();<br>            }<br>        }<br>   

좋은 웹페이지 즐겨찾기