js 獲取/設置文本輸入域內光標的位置的方法


    /**
     * 設置光標在短連接輸入框中的位置
     * @param inputId 框Id
     * @param pos
     * @return {*}s
     */
    function setCursorPos(inputId, pos){

        var inpObj = document.getElementById(inputId);
        if(navigator.userAgent.indexOf("MSIE") > -1){
            var range = document.selection.createRange();
            var textRange = inpObj.createTextRange();
            textRange.moveStart('character',pos);
            textRange.collapse();
            textRange.select();
        }else{
            inpObj.setSelectionRange(n,n);
        }
    }


    /**
     * 獲取光標在短連接輸入框中的位置
     * @param inputId 框Id
     * @return {*}
     */
    function getCursorPos(inputId){
        var inpObj = document.getElementById(inputId);
         if(navigator.userAgent.indexOf("MSIE") > -1) { // IE
            var range = document.selection.createRange();
            range.text = '';
            range.setEndPoint('StartToStart',inpObj.createTextRange());
            return range.text.length;
        } else {
            return inpObj.selectionStart;
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM