.net中js方法,设置在文本框中光标位置处插入数据

来源:互联网 发布:软件测试面试技巧 编辑:程序博客网 时间:2024/05/22 04:41
 function setContent(str) //str为要传的参数
  {
  var obj = document.getElementById('txtFormContent'); //获取文本框
     if(document.selection) {  //为空时赋值
      obj.focus(); 
      var sel=document.selection.createRange(); 
       document.selection.empty(); 
       sel.text = str; 
        } 
    else
       { //文本框里有值时赋值
        var prefix, main, suffix; 
        prefix = obj.value.substring(0, obj.selectionStart); 
        main = obj.value.substring(obj.selectionStart, obj.selectionEnd); 
        suffix = obj.value.substring(obj.selectionEnd); 
        obj.value = prefix +str+ suffix; 
       }
     obj.focus(); 
  }
原创粉丝点击