jQuery Textarea focus

后端 未结 3 2002
青春惊慌失措
青春惊慌失措 2021-01-07 17:43

When I click a button I want the textarea in this li element to focus.

  • 相关标签:
    3条回答
    • 2021-01-07 18:14

      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 #StatusMessageReplyMessage element focus after the slide effect has finished.

      0 讨论(0)
    • 2021-01-07 18:16

      The easiest solution is to use 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 :)

      0 讨论(0)
    • 2021-01-07 18:24

      I use timer to focus text areas :

      setTimeout(function() {
       $el.find('textarea').focus();
      }, 0);
      
      0 讨论(0)
    提交回复
    热议问题