有个同事下午的时候提出这个问题,没有人解答,结果他在百度找到了~
我也贴出来,做个记号,可能以后要用到呢。

如何取得TEXTBOX里的光标位置function getCaret(textbox) 
}

在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();
}

相关文章: