jQuery Textarea 焦点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4191200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:48:30  来源:igfitidea点击:

jQuery Textarea focus

jqueryfocustextarea

提问by Harsha M V

When I click a button I want the textareain this lielement to focus.

当我点击一个按钮时,我希望textarea这个li元素中的 聚焦。

<li class="commentBlock" id="commentbox-79" style="display: list-item;">
  <div>
    <div class="grid userImageBlockS">
      <div class="imgSmall">
        <img width="35" height="35" alt="image" src="/bakasura1/files/images/small/1288170363aca595cabb50.jpg">
      </div>
    </div>
    <div class="grid userContentBlockS alpha omega"> 
      <form accept-charset="utf-8" action="/bakasura1/account/saveComment" method="post">
        <div style="display: none;">
          <input type="hidden" value="POST" name="_method">
        </div> 
        <input type="hidden" id="StatusMessageReplyPid" value="79" name="data[StatusMessageReply][pid]"> 
        <input type="hidden" id="StatusMessageReplyItemId" value="1" name="data[StatusMessageReply][item_id]"> 
        <input type="hidden" id="StatusMessageReplyCommentersItemId" value="1" name="data[StatusMessageReply][commenters_item_id]"> 
        <textarea id="StatusMessageReplyMessage" name="data[StatusMessageReply][message]">
          Write your comment...
        </textarea>
        <input type="submit" name="" value="Comment" class="comment" id="comment-79">
      </form> 
    </div>
    <div class="clear"></div>
  </div>
</li>

This is my jQuerycode:

这是我的jQuery代码:

$('.user-status-buttons').click(function() {
    var id = $(this).attr('id');
    $("#commentbox-"+id).slideToggle("fast");
    $("#commentbox-"+id+" #StatusMessageMessage").focus();
    return false;
});

回答by Roman Yudin

I use timer to focus text areas :

我使用计时器来聚焦文本区域:

setTimeout(function() {
 $el.find('textarea').focus();
}, 0);

回答by Horatio Alderaan

Based on your comment in reply to Jacob, perhaps you want:

根据您对雅各布的评论,也许您想要:

$('.user-status-buttons').click(function(){

    var id = $(this).attr('id');
    $("#commentbox-"+id).slideToggle("fast", function(){
        $("#commentbox-"+id+" #StatusMessageReplyMessage").focus();
    });

    return false;
});

This should give the #StatusMessageReplyMessageelement focus afterthe slide effect has finished.

这应该在幻灯片效果完成给予#StatusMessageReplyMessage元素焦点。

回答by zod

The easiest solution is to use jQuery focus

最简单的解决方案是使用jQuery focus

  $('#StatusMessageReplyMessage').focus();

NOTE: if you are testing this in the console, Chrome will send the focus back to the console! This can lead you to believe it had not worked when in fact it works perfectly. Just be aware of other focus grabbing scripts/behavior in your environment and it will all be fine :)

注意:如果您在控制台中进行测试,Chrome 会将焦点发送回控制台!这可能会导致您认为它没有工作,而实际上它工作得很好。请注意您环境中的其他焦点抓取脚本/行为,一切都会好起来的:)