複製程式碼 程式碼如下: <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> 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("c selectionStart){//非IE瀏覽器<br> oTa2.selection開始=我; <br> oTa2.selectionEnd=i;<br> TextRange();<br> range.move("character", );<br> );<br> }<br> }<br> <br><br><br> </p> </div>