01 | function getTxt1CursorPosition(){ |
02 | var oTxt1 = document.getElementById( "txt1" ); |
03 | var cursurPosition=-1; |
04 | if (oTxt1.selectionStart){ //非IE浏览器 |
05 | cursurPosition= oTxt1.selectionStart; |
06 | } else { //IE |
07 | var range = document.selection.createRange(); |
08 | range.moveStart( "character" ,-oTxt1.value.length); |
09 | cursurPosition=range.text.length; |
10 | } |
11 | alert(cursurPosition); |
12 | } |
13 | function setTxt1CursorPosition(i){ |
14 | var oTxt1 = document.getElementById( "txt2" ); |
15 | var cursurPosition=-1; |
16 | if (oTxt1.selectionStart){ //非IE浏览器 |
17 | oTxt1.selectionStart=i; |
18 | } else { //IE |
19 | var range = oTxt1.createTextRange(); |
20 | range.move( "character" ,i); |
21 | range.select(); |
22 | } |
23 | } |
24 | function getTa1CursorPosition(){ |
25 | var evt =window.event?window.event:getTa1CursorPosition.caller.arguments[0]; |
26 | var oTa1 = document.getElementById( "ta1" ); |
27 | var cursurPosition=-1; |
28 | if (oTa1.selectionStart){ //非IE浏览器 |
29 | cursurPosition= oTa1.selectionStart; |
30 | } else { //IE |
31 | var range = oTa1.createTextRange(); |
32 | range.moveToPoint(evt.x,evt.y); |
33 | range.moveStart( "character" ,-oTa1.value.length); |
34 | cursurPosition=range.text.length; |
35 | } |
36 | alert(cursurPosition); |
37 | } |
38 | function setTa1CursorPosition(i){ |
39 | var oTa2 = document.getElementById( "ta2" ); |
40 | if (oTa2.selectionStart){ //非IE浏览器 |
41 | oTa2.selectionStart=i; |
42 | oTa2.selectionEnd=i; |
43 | } else { //IE |
44 | var range = oTa2.createTextRange(); |
45 | range.move( "character" ,i); |
46 | range.select(); |
47 | } |
48 | } |