<HTML>
<HEAD>
<SCRIPT>
function storeCaret (textEl) {
if (textEl.createTextRange)
textEl.caretPos = document.selection.createRange().duplicate();
}
function insertAtCaret (textEl, text) {
if (textEl.createTextRange && textEl.caretPos) {
var caretPos = textEl.caretPos;
caretPos.text =
caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
text + ' ' : text;
}
else
textEl.value = text;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="aForm">
<TEXTAREA NAME="TextArea1" ROWS="5" COLS="80" WRAP="soft"
ONSELECT= "storeCaret(this);"
ONCLICK= "storeCaret(this);"
ONKEYUP= "storeCaret(this);"
>
請輸入任意內容.
</TEXTAREA>
<BR>
<INPUT TYPE="text" NAME="aText" SIZE="80" VALUE="Mars,加油!">
<BR>
<INPUT TYPE="button" VALUE="插入" ONCLICK="insertAtCaret(this.form.TextArea1, this.form.aText.value);">
</FORM>
</BODY>
</HTML>
出處:如何在 TextArea 中,任一游標處插入文字