2013-06-29 67 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

它不适用于