2013-06-29 68 views

回答

0

試試這個:

$(document).ready(function(){ 
     $("#search").focus(function(){ 
      if (this.setSelectionRange) 
      { 
      var len = $(this).val().length; 
      this.setSelectionRange(len, len); 
      } 
      else 
      { 
      $(this).val($(this).val()); 
      } 

    }); 

    $("#search").focus(); 
    }); 

Demo with input

Demo with textarea

因爲setSelectionRange並不被所有的瀏覽器都支持。所以最好的解決方案是結合setSelectionRange$(this).val($(this).val());。欲瞭解更多信息,請訪問:Use JavaScript to place cursor at end of text in text input element

+0

它在開始時設置位置,但我希望它在文本的末尾 – nrsharma

+0

@nrsharma:結帳演示。我使用Chrome進行了測試,它的工作原理 –

+1

它不適用於