下面是代码: 复制代码 代码如下: -------------------------------------------------------------------------------- JS在文本域鼠标指定位置插入文本-柯乐义 <BR>function insertAtCursor(myField, myValue) {<BR>//IE support<BR>if (document.selection) {<BR>myField.focus();<BR>sel = document.selection.createRange();<BR>sel.text = myValue;<BR>sel.select();<BR>}<BR>//MOZILLA/NETSCAPE support <BR>else if (myField.selectionStart || myField.selectionStart == '0') {<BR>var startPos = myField.selectionStart;<BR>var endPos = myField.selectionEnd;<BR>// save scrollTop before insert www.keleyi.com<BR>var restoreTop = myField.scrollTop;<BR>myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);<BR>if (restoreTop > 0) {<BR>myField.scrollTop = restoreTop;<BR>}<BR>myField.focus();<BR>myField.selectionStart = startPos + myValue.length;<BR>myField.selectionEnd = startPos + myValue.length;<BR>} else {<BR>myField.value += myValue;<BR>myField.focus();<BR>}<BR>} <BR> 柯乐义 Javascript 在textarea光标处插入文本