Autofocus on input field when div slides down

后端 未结 4 1532
傲寒
傲寒 2021-02-13 11:11

I have is a hidden div. When the button is clicked, the div slides down with an input field. How do I make this input field autofocus as soon as the div slides down? Thanks.

4条回答
  •  忘掉有多难
    2021-02-13 11:22

    You can use slideDown's callback function which is executed when animation is complete.

    $("#status_comments_"+a).slideDown(function(){
        $("#comment_field").focus();
    });
    

    Note that IDs must be unique, otherwise your ID selector only selects the first element with that specific ID. If input element is within the div tag you can also code:

    $("#status_comments_"+a).slideDown(function(){
        $('input', this).focus();
    });
    

提交回复
热议问题