开发者

Move focus to a particular field

开发者 https://www.devze.com 2023-01-10 03:00 出处:网络
I have a button th开发者_运维问答at will add show a form on the page. how can I move the focus to the first field of the form when that button is clicked?

I have a button th开发者_运维问答at will add show a form on the page. how can I move the focus to the first field of the form when that button is clicked?

simple example:

HTML:

<form style="display:none;" id="newForm">
   <input type="text" id="firstField">
</form>
<input type="button" id="showForm" value="add new">

jQuery:

 $("#showForm").click(function(){
     $("#newForm").show();
     //move focus??
});


It might be this:

$("#newForm input:first").focus();


A quicker lookup would simply be.

$('#firstField').focus()

However if you removed the ID from your element then this would be better but slightly slower.

$('#newForm input:first').focus();


Try this:

$('input#firstfield').focus();
0

精彩评论

暂无评论...
验证码 换一张
取 消