我也贴出来,做个记号,可能以后要用到呢。
function getCaret(textbox)
{
var control = document.activeElement;
textbox.focus();
var rang = document.selection.createRange();
rang.setEndPoint("StartToStart",textbox.createTextRange())
control.focus();
pos.innerText = rang.text.length;
return rang.text.length;
}
{
var control = document.activeElement;
textbox.focus();
var rang = document.selection.createRange();
rang.setEndPoint("StartToStart",textbox.createTextRange())
control.focus();
pos.innerText = rang.text.length;
return rang.text.length;
}
在Textbox里光标的位置添加字符串:
function insertunit(text, textend)
{
$('txtFormular').focus();
textend = isUndefined(textend) ? '' : textend;
if(!isUndefined($('txtFormular').selectionStart))
{
var opn = $('txtFormular').selectionStart + 0;
if(textend != '')
{
text = text + $('txtFormular').value.substring($('txtFormular').selectionStart, $('txtFormular').selectionEnd) + textend;
}
$('txtFormular').value = $('txtFormular').value.substr(0, $('txtFormular').selectionStart) + text + $('txtFormular').value.substr($('txtFormular').selectionEnd);
}
else if(document.selection && document.selection.createRange)
{
var sel = document.selection.createRange();
if(textend != '')
{
text = text + sel.text + textend;
}
sel.text = text.replace(/\r?\n/g, '\r\n');
//sel.moveStart('character', -strlen(text));
}
else
{
$('txtFormular').value += text;
}
//formulaexp();
}
{
$('txtFormular').focus();
textend = isUndefined(textend) ? '' : textend;
if(!isUndefined($('txtFormular').selectionStart))
{
var opn = $('txtFormular').selectionStart + 0;
if(textend != '')
{
text = text + $('txtFormular').value.substring($('txtFormular').selectionStart, $('txtFormular').selectionEnd) + textend;
}
$('txtFormular').value = $('txtFormular').value.substr(0, $('txtFormular').selectionStart) + text + $('txtFormular').value.substr($('txtFormular').selectionEnd);
}
else if(document.selection && document.selection.createRange)
{
var sel = document.selection.createRange();
if(textend != '')
{
text = text + sel.text + textend;
}
sel.text = text.replace(/\r?\n/g, '\r\n');
//sel.moveStart('character', -strlen(text));
}
else
{
$('txtFormular').value += text;
}
//formulaexp();
}