简体   繁体   English

jQuery单击不适用于html输入

[英]jQuery click doesn't work on html input

I have an input 我有输入

<input type='text' id='email'/>

For some event I'd like to focus on that textbox 对于某些事件,我想专注于该文本框

$('#email').click()

But this code doesn't work for some reasons (Chrome). 但是由于某些原因,此代码无法正常使用(Chrome)。 How can I achieve my goal? 我如何实现我的目标?

Instead of click you should use focus() to have a focus on the desired target element and make sure to wrap it inside doc ready block: 而不是click您应该使用focus()将焦点放在所需的目标元素上,并确保将其包装在doc ready块中:

$(function(){ // <-------doc ready
   $('#email').focus(); // <----use focus event
});

Document ready ensures that DOM is ready and can be used. 准备好文档可确保DOM准备就绪并可以使用。 Either you wrap your js code inside this block or put the script at the bottom of the page. 您可以将js代码包装在此块中,也可以将脚本放在页面底部。 which runs when whole page gets loaded. 当整个页面加载时运行。


If you are using HTML5 then you might look into autofocus attribute: 如果您使用的是HTML5,则可以查看autofocus属性:

 <input type='text' id='email' autofocus /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM