<html> 
<head> 
<script type='text/javascript'> 
function test(str){    var tc = document.getElementById("mytextarea");
    var tclen = tc.value.length;
    tc.focus();
    if(typeof document.selection != "undefined")
    {
        document.selection.createRange().text = str;  
    }
    else
    {
        tc.value = tc.value.substr(0,tc.selectionStart)+str+tc.value.substring(tc.selectionStart,tclen);
    }
}
</script> 
</head> 
<body>
<textarea rows=5 name=s1 cols=27  id="mytextarea">目的通过点击页面上的按钮button 在textarea中的光标停留处插上文字 </textarea> 
<input type=button onclick="test('这是需要加入的文字')" value="追加" /> 
</body> 
</html>

解决方案 »

  1.   

    用 var sel=document.selection.createRange();
    document.getElementById("xx").value=sel.text
      

  2.   

    /*在当前光标处插入字符*/
    function insertAtCaret(text) 
    {
    if(onFocusTextArea==null)
    {
    alert('请选择要输入的文本框!');
    return;
    }
    if (onFocusTextArea.createTextRange && onFocusTextArea.caretPos) 
    {
    var caretPos = onFocusTextArea.caretPos;
    caretPos.text =caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?text + ' ' : text; 

    else 
    onFocusTextArea.value = text;
      

  3.   

    谢谢大家的帮助!我现在应经找到答案了!
    答案原理如下<textarea name="Content" id="Content" cols="50" rows="5" class="iptxt" onSelect="javascript: storeCaret(this);" onClick="javascript: storeCaret(this);" onKeyUp="javascript: storeCaret(this);" AUTOCOMPLETE="off">function InsertTag(ControlID, Text)
    {
    var objElement;
    objElement = document.getElementById(ControlID);
    if (objElement.createTextRange && objElement.caretPos)
    {
    var caretPos1 = objElement.caretPos;
    caretPos1.text = Text;
    objElement.focus();

    else 
    {
    objElement.value  += Text;
    objElement.focus();
    }
    return false;
    }
    function storeCaret (textEl){        if(textEl.createTextRange){
                    textEl.caretPos = document.selection.createRange().duplicate();
            }
    }