zoukankan      html  css  js  c++  java
  • 如何取得TEXTBOX里的光标位置

    有个同事下午的时候提出这个问题,没有人解答,结果他在百度找到了~
    我也贴出来,做个记号,可能以后要用到呢。

    function getCaret(textbox) 

     
    var control = document.activeElement; 
     textbox.focus(); 
     
    var rang = document.selection.createRange(); 
      rang.setEndPoint(
    "StartToStart",textbox.createTextRange()) 
     control.focus();
     pos.innerText 
    = rang.text.length;
     
    return rang.text.length; 
    }

    在Textbox里光标的位置添加字符串:
    function insertunit(text, textend)
    {
        $('txtFormular').focus();
        textend 
    = isUndefined(textend) ? '' : textend;
        
    if(!isUndefined($('txtFormular').selectionStart))
        {
            
    var opn = $('txtFormular').selectionStart + 0;
            
    if(textend != '')
            {
                text 
    = text + $('txtFormular').value.substring($('txtFormular').selectionStart, $('txtFormular').selectionEnd) + textend;
            }
            
            $('txtFormular').value 
    = $('txtFormular').value.substr(0, $('txtFormular').selectionStart) + text + $('txtFormular').value.substr($('txtFormular').selectionEnd);
        }
        
    else if(document.selection && document.selection.createRange)
        {
            
    var sel = document.selection.createRange();
            
    if(textend != '')
            {
                text 
    = text + sel.text + textend;
            }
            sel.text 
    = text.replace(/\r?\n/g, '\r\n');
            
    //sel.moveStart('character', -strlen(text));
        }
        
    else
        {
            $('txtFormular').value 
    += text;
        }
        
    //formulaexp();
    }
  • 相关阅读:
    android adb常用命令
    weblogic 优化设置 http://wenku.baidu.com/view/c42e7a5bbe23482fb4da4cf2.html
    Ajax+json实现菜单动态级联
    浏览器兼容性
    登录相关
    Java 日期处理
    QBC分页查询
    WebService
    MD5加密
    JSON与JAVA数据的转换
  • 原文地址:https://www.cnblogs.com/seamusic/p/964478.html
Copyright © 2011-2022 走看看