zoukankan      html  css  js  c++  java
  • 鼠标点击textarea后,在光标后追加内容

    $("#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();
         }
    })

     

  • 相关阅读:
    String ,StringBuilder, StringBuffer
    apt-get方式删除软件
    curl命令的使用
    maven自动部署测试Web应用
    几个重要的maven命令
    linux中默认jdk的配置
    登录注册的页面制作
    运用php做投票题,例题
    复选框式查询 例题租房子
    会话用法 和留言板例题
  • 原文地址:https://www.cnblogs.com/sunBinary/p/10104579.html
Copyright © 2011-2022 走看看