操作切换时自动选择输入字段[重复]

This question already has an answer here:

I have a search bar that only displayed when the toggle button is clicked. The code I am using to do this is:

$("a.search-toggle").click(function(){
    $(".search-bar").slideToggle(200);
    $(this).toggleClass("open");
});

Now I am trying to find a way to auto select the input[type="search"] field when this toggle is actioned. Currently the user has to click the toggle button, then click the search field which isn't very user friendly.

Note

I am not looking for this to occur on page load, only once toggle has been activated.

</div>

This should do it

$('input[type="search"]').focus();

See the docs here