2012-11-10 115 views
-1

如何檢查光標位置是在文本的前面還是在文本的最後。獲取文本框光標位置(文本之前或之後)

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('input').keyup(function (e) { 
      if (e.which == 39 && <CURSOR IS FIRST OR LAST OF TEXT>) { 
       alert($('input').val); 
       $(this).closest('td').next().find('input').focus(); 
       $(this).closest('td').next().find('input').select(); 
      } 
     } 
     } 
<script> 


<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script> 

<script type="text/javascript"> 
$(document).ready(function() { 
    $('input').keyup(function (e) { 
     if (e.which == 39 && <selected text lenght> > 0) { 
      $(this).closest('td').next().find('input').focus(); 
      $(this).closest('td').next().find('input').select(); 
     } 
     else if (e.which == 37) { 
      $(this).closest('td').prev().find('input').focus(); 
      $(this).closest('td').prev().find('input').select(); 
     } 
     else if (e.which == 40) { 
      $(this).closest('tr').next().find('td:eq(' + $(this).closest('td').index() + ')').find('input').focus(); 
      $(this).closest('tr').next().find('td:eq(' + $(this).closest('td').index() + ')').find('input').select(); 
     } 
     else if (e.which == 38) { 
      $(this).closest('tr').prev().find('td:eq(' + $(this).closest('td').index() + ')').find('input').focus(); 
      $(this).closest('tr').prev().find('td:eq(' + $(this).closest('td').index() + ')').find('input').select(); 
     } 
    }); 
}); 

回答

2

您可以使用jQuery插件插入符。檢查this thread herethe plugin's homepage。然後,您可以更改您的代碼:

var pos= $(this).caret().start == $(this).caret().end ? $(this).caret().start : -1; 
if (e.which === 39 && (pos === 0 || pos === $(this).val().length)) { 
... 

希望它能幫助, Shakib

相關問題