Maison > Article > interface Web > javascript在一段文字中的光标处插入其他文字_表单特效
例如:一个文本框里有一段文字,我要通过点击鼠标并且在相应的地方插入文本
我是这样做的:先获得文本框的值TextArea.Value,然后.TextArea.Value+其他文本
<script> <BR>function setCaret(textObj){ <BR> if(textObj.createTextRange){ <BR> textObj.caretPos=document.selection.createRange().duplicate(); <BR> } <BR>} <br><br>function insertAtCaret(textObj,textFeildValue){ <BR> if(document.all){ <BR> if(textObj.createTextRange&&textObj.caretPos){ <BR> var caretPos=textObj.caretPos; <BR> caretPos.text=caretPos.text.charAt(caretPos.text.length-1)==''?textFeildValue+'':textFeildValue; <BR> }else { <BR> textObj.value=textFeildValue; <BR> } <BR> }else { <BR> if(textObj.setSelectionRange){ <BR> var rangeStart=textObj.selectionStart; <BR> var rangeEnd=textObj.selectionEnd; <BR> var tempStr1=textObj.value.substring(0,rangeStart); <BR> var tempStr2=textObj.value.substring(rangeEnd); <BR> textObj.value=tempStr1+textFeildValue+tempStr2; <BR> }else { <BR> alert("This version of Mozilla based browser does not support setSelectionRange"); <BR> } <BR> } <BR>} <BR></script>