zoukankan      html  css  js  c++  java
  • jQuery 文本框 光标 移动到 文字最后

    方法一:调用办法:setCaretToPos(document.getElementById("YOURINPUT"), 4);

     1     function setSelectionRange(input, selectionStart, selectionEnd) {  
     2       if (input.setSelectionRange) {  
     3         input.focus();  
     4         input.setSelectionRange(selectionStart, selectionEnd);  
     5       }  
     6       else if (input.createTextRange) {  
     7         var range = input.createTextRange();  
     8         range.collapse(true);  
     9         range.moveEnd('character', selectionEnd);  
    10         range.moveStart('character', selectionStart);  
    11         range.select();  
    12       }  
    13     }  
    14       
    15     function setCaretToPos (input, pos) {  
    16       setSelectionRange(input, pos, pos);  
    17     }  

    方法二:调用办法:$('#elem').selectRange(3,5);

     1     $.fn.selectRange = function(start, end) {  
     2         return this.each(function() {  
     3             if (this.setSelectionRange) {  
     4                 this.focus();  
     5                 this.setSelectionRange(start, end);  
     6             } else if (this.createTextRange) {  
     7                 var range = this.createTextRange();  
     8                 range.collapse(true);  
     9                 range.moveEnd('character', end);  
    10                 range.moveStart('character', start);  
    11                 range.select();  
    12             }  
    13         });  
    14     };  

    方法三:调用办法:$(element).focusEnd();

     1     $.fn.setCursorPosition = function(position){  
     2         if(this.lengh == 0) return this;  
     3         return $(this).setSelection(position, position);  
     4     }  
     5       
     6     $.fn.setSelection = function(selectionStart, selectionEnd) {  
     7         if(this.lengh == 0) return this;  
     8         input = this[0];  
     9       
    10         if (input.createTextRange) {  
    11             var range = input.createTextRange();  
    12             range.collapse(true);  
    13             range.moveEnd('character', selectionEnd);  
    14             range.moveStart('character', selectionStart);  
    15             range.select();  
    16         } else if (input.setSelectionRange) {  
    17             input.focus();  
    18             input.setSelectionRange(selectionStart, selectionEnd);  
    19         }  
    20       
    21         return this;  
    22     }  
    23       
    24     $.fn.focusEnd = function(){  
    25         this.setCursorPosition(this.val().length);  
    26     }  
  • 相关阅读:
    html表格,table标签
    2-3VRP的基本配置
    6 sys模块
    3 datetime模块
    2 time模块
    1 模块和包的介绍
    12 函数进阶---生成器
    13 函数进阶---迭代器
    10 函数进阶---闭包
    11 函数进阶---装饰器
  • 原文地址:https://www.cnblogs.com/linvan/p/8793194.html
Copyright © 2011-2022 走看看