<script type="text/javascript">
function changes(obj){
var TextIn=document.getElementById("textin");
if(TextIn.createTextRange){
//ie兼容
TextIn.focus();
document.selection.createRange().duplicate().text=obj;
}else{
//firefox兼容
var iStart = TextIn.selectionStart;
var iEnd = TextIn.selectionEnd;
TextIn.value=TextIn.value.substring(0,iStart)+obj+TextIn.value.substring(iEnd,TextIn.value.length);
endRange=iStart+obj.length;
TextIn.setSelectionRange(endRange,endRange);
}
}
</script>
<textarea cols="40" id="textin">选中这里的文字,然后点change试试看。</textarea>
<br />
<input type="button" onclick="changes('文字被替换了!')" value="change" />