获得TextBox中光标的位置(get the cursor position in a TextBox or InputText)
使用JavaScript实现:
function getposition()
{
var txt1=document.getElementById("TextBox1");
var currentRange=document.selection.createRange();
var workRange=currentRange.duplicate();
txt1.select();
var allRange=document.selection.createRange();
var len=0;
while(workRange.compareEndPoints("StartToStart",allRange)>0)
{
workRange.moveStart("character",-1);
len++;
}
currentRange.select();
window.alert(len);
}
{
var txt1=document.getElementById("TextBox1");
var currentRange=document.selection.createRange();
var workRange=currentRange.duplicate();
txt1.select();
var allRange=document.selection.createRange();
var len=0;
while(workRange.compareEndPoints("StartToStart",allRange)>0)
{
workRange.moveStart("character",-1);
len++;
}
currentRange.select();
window.alert(len);
}