html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
javascript获取/设置 文本框/文本域中的光标位置functiongetTxt1CursorPosition(){varoTxt1=document.getElementById("txt1");varcursurPosition=-1;if(oTxt1.selectionStart){//非IE浏览器cursurPosition=oTxt1.selectionStart;}else{//IEvarrange=document.selection.createRange();
range.moveStart("character",-oTxt1.value.length);
cursurPosition=range.text.length;
}
alert(cursurPosition);
}functionsetTxt1CursorPosition(i){varoTxt1=document.getElementById("txt2");varcursurPosition=-1;if(oTxt1.selectionStart){//非IE浏览器oTxt1.selectionStart=i;
}else{//IEvarrange=oTxt1.createTextRange();
range.move("character",i);
range.select();
}
}functiongetTa1CursorPosition(){varevt=window.event?window.event:getTa1CursorPosition.caller.arguments[0];varoTa1=document.getElementById("ta1");varcursurPosition=-1;if(oTa1.selectionStart){//非IE浏览器cursurPosition=oTa1.selectionStart;
}else{//IEvarrange=oTa1.createTextRange();
range.moveToPoint(evt.x,evt.y);
range.moveStart("character",-oTa1.value.length);
cursurPosition=range.text.length;
}
alert(cursurPosition);
}functionsetTa1CursorPosition(i){varoTa2=document.getElementById("ta2");if(oTa2.selectionStart){//非IE浏览器oTa2.selectionStart=i;
oTa2.selectionEnd=i;
}else{//IEvarrange=oTa2.createTextRange();
range.move("character",i);
range.select();
}
}
abcdefg
hijklmn
opqrst
uvwxyz
点击我获取文本域的光标位置abcdefg
hijklmn
opqrst
uvwxyz
点击我设置文本域的光标位置为30