textarea 光标安插
textarea 光标插入
<textarea id='target_area'onclick="setCaret(this);" onkeyup="setCaret(this);"></textarea>
//设置textarea光标位置 function setCaret(textObj){ if(textObj.createTextRange){ textObj.caretPos=document.selection.createRange().duplicate(); } } //光标位置插入内容 function insertAtCaret(textFeildValue){ var textObj = document.getElementById('target_area'); if(firefox){ var start = textObj.selectionStart; var end = textObj.selectionEnd; textObj.value = textObj.value.substr(0,start) + textFeildValue + textObj.value.substr(end); } else { if(textObj.createTextRange && textObj.caretPos){ var caretPos=textObj.caretPos; caretPos.text=caretPos.text.charAt(caretPos.text.length-1)==''?textFeildValue+'':textFeildValue; }else { textObj.value+=textFeildValue; } } }