コードをコピー コードは次のとおりです: <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("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>