div向下滑动时在输入字段上自动对焦 [英] Autofocus on input field when div slides down

查看:155
本文介绍了div向下滑动时在输入字段上自动对焦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个隐藏的div。当单击按钮时,div随着输入字段向下滑动。一旦div滑动下来,我如何使这个输入字段自动对焦?感谢。

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.

<input type="text" name="comment_field" id="comment_field" placeholder="Comment..." />

是我所拥有的,位于div

is what I have and is in a div

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

我试图使用上面的,但是没有工作。

I tried to use the above, but that didn't work.

推荐答案

您可以使用 slideDown 的回调函数动画完成。

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

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

请注意,ID必须是唯一的,否则ID选择器只选择具有该特定ID的第一个元素。如果输入元素在div标签中,您还可以编码:

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

这篇关于div向下滑动时在输入字段上自动对焦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆