$("#insertMsg").on("click",function(){ //获取下拉选项框的值 var textFeildValue = $("#text_type").find("option:selected").text(); //获取textarea的值 var myField=document.getElementById("text_smsnr"); if (document.selection) {//IE浏览器 myField.focus(); sel = document.selection.createRange(); sel.text = "{"+textFeildValue+"}"; sel.select(); }else if (myField.selectionStart || myField.selectionStart == '0'){//火狐/网景 浏览器 //得到光标前的位置 var startPos = myField.selectionStart; //得到光标后的位置 var endPos = myField.selectionEnd; // 在加入数据之前获得滚动条的高度 var restoreTop = myField.scrollTop; myField.value = myField.value.substring(0, startPos) + "{" + textFeildValue +"}" + myField.value.substring(endPos, myField.value.length); //如果滚动条高度大于0 if (restoreTop > 0) { // 返回 myField.scrollTop = restoreTop; } // myField.focus(); myField.selectionStart = startPos + "{"+textFeildValue.length+"}"; myField.selectionEnd = startPos + textFeildValue.length; } else { myField.value += "{"+textFeildValue+"}"; // myField.focus(); } })