例如:一个文本框里有一段文字,我要通过点击鼠标并且在相应的地方插入文本
我是这样做的:先获得文本框的值TextArea.Value,然后.TextArea.Value+其他文本
<scripttype="text/javascript">
functionsetCaret(textObj){
if(textObj.createTextRange){
textObj.caretPos=document.selection.createRange().duplicate();
}
}
functioninsertAtCaret(textObj,textFeildValue){
if(document.all){
if(textObj.createTextRange&&textObj.caretPos){
varcaretPos=textObj.caretPos;
caretPos.text=caretPos.text.charAt(caretPos.text.length-1)==''?textFeildValue+'':textFeildValue;
}else{
textObj.value=textFeildValue;
}
}else{
if(textObj.setSelectionRange){
varrangeStart=textObj.selectionStart;
varrangeEnd=textObj.selectionEnd;
vartempStr1=textObj.value.substring(0,rangeStart);
vartempStr2=textObj.value.substring(rangeEnd);
textObj.value=tempStr1+textFeildValue+tempStr2;
}else{
alert("ThisversionofMozillabasedbrowserdoesnotsupportsetSelectionRange");
}
}
}
</script>
<formid="form1"action=""onsubmit=""method="post"enctype="text/plain">
<p>
<textareaname="tarea"rows=""cols=""style="width:300px;height:120px;"onselect="setCaret(this);"onclick="setCaret(this);"onkeyup="setCaret(this);">
Dnew.cnDnew.cn
</textarea>
<br/><br/>
<inputtype="text"name="textfield"style="width:220px;"value="插入FireFox"/>
<br/>
<inputtype="button"value="插入"onclick="insertAtCaret(this.form.tarea,this.form.textfield.value);"/>
</p>
</form>