博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jQuery设置聚焦并使光标位置在文字最后
阅读量:2229 次
发布时间:2019-05-09

本文共 1750 字,大约阅读时间需要 5 分钟。

方法一:

[javascript]
  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. }  

 

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

方法二:

[javascript]
  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. };  

 

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

方法三:

[javascript]
  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. }  

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

阻止某些文字被选中

$.fn.disableSelection

        ".ui-disableSelection", function( event ) {

            event.preventDefault();
        });

}

 

转载于:https://www.cnblogs.com/interdrp/p/6758062.html

你可能感兴趣的文章
【LEETCODE】215-Kth Largest Element in an Array
查看>>
【LEETCODE】241-Different Ways to Add Parentheses
查看>>
【LEETCODE】312-Burst Balloons
查看>>
【LEETCODE】232-Implement Queue using Stacks
查看>>
【LEETCODE】225-Implement Stack using Queues
查看>>
【LEETCODE】155-Min Stack
查看>>
【LEETCODE】20-Valid Parentheses
查看>>
【LEETCODE】290-Word Pattern
查看>>
【LEETCODE】36-Valid Sudoku
查看>>
【LEETCODE】205-Isomorphic Strings
查看>>
【LEETCODE】204-Count Primes
查看>>
【LEETCODE】228-Summary Ranges
查看>>
【LEETCODE】27-Remove Element
查看>>
【LEETCODE】66-Plus One
查看>>
【LEETCODE】26-Remove Duplicates from Sorted Array
查看>>
【LEETCODE】118-Pascal's Triangle
查看>>
【LEETCODE】119-Pascal's Triangle II
查看>>
【LEETCODE】190-Reverse Bits
查看>>
【LEETCODE】223-Rectangle Area
查看>>
【LEETCODE】12-Integer to Roman
查看>>