0

您可以轻松地从站点或任何站点找到 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 中它从开始开始光标。任何解决方案?

4

2 回答 2

0

看看这个问题:jQuery Set Cursor Position in Text Area

于 2009-10-27T22:03:43.107 回答
0

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

$("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();
   }
});

希望对你有帮助,思南。

于 2009-10-27T23:49:14.697 回答