【问题标题】:jquery focus, but with more accurate postionjquery 焦点,但位置更准确
【发布时间】:2009-10-27 21:59:06
【问题描述】:

您可以从网站或任何网站轻松找到 focus() 答案。但我想知道的是,在我刚刚插入输入框后如何使用焦点。

$("input").val('@user').focus(); // this goes to start of the input box and foucs from start, but i want it to focus after the @user

$("input").val('@user today i did new things').focus(5); // i added 5 in focus method, obviously its not supported yet!, but the idea behind is, to focus it after @user from complete word.

已编辑:

我刚刚在 Firefox 上看到它工作正常,但在 chrome 中它从头开始启动光标。有什么解决办法吗?

【问题讨论】:

    标签: javascript jquery focus mouse cursor


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      您可以通过在更改事件上触发输入区域来聚焦:

      $("input").change(function(){
         $(this).focus();
      });
      

      如果你想检查输入了多少个字符,你可以对其回调进行控制:

      //will focus if typed text's length is greater than 5
      $("input").change(function(){
         if($(this).val().length > 5){
           $(this).focus();
         }
      });
      

      希望对你有帮助,思南。

      【讨论】:

      • 它不会在单词之后聚焦。它会在单词之前聚焦。之后我需要它集中注意力
      • 我认为您的意思是在此处设置插入符号位置,而不是专注于输入元素。无论如何,Seb 答案中的链接是一个很好的资源。
      猜你喜欢
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多