<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>
<FORM name=aForm>
<TEXTAREA onkeyup=storeCaret(this); onclick=storeCaret(this); name=aTextArea rows=5 cols=80 onselect=storeCaret(this);>测试测试测试测试测试测试</TEXTAREA>
<INPUT size=80 value=要插入的文字 name=aText>
<INPUT onclick=insertAtCaret(this.form.aTextArea,this.form.aText.value); type=button value=在光标处插入>
</FORM>
博客展示了一段JavaScript代码,定义了storeCaret和insertAtCaret两个函数,用于存储文本框光标位置和在光标处插入文字。同时给出了HTML表单,包含文本框、输入框和按钮,通过点击按钮可实现文字插入功能。
1803

被折叠的 条评论
为什么被折叠?



