简体   繁体   中英

JQuery focus an element

In javascript the following works to give focus to the edit_2 input box:

document.getElementById("edit_2").focus();

However using Jquery this does not:

$("#edit_2").focus;

You are calling a method, so:

$("#edit_2").focus;

should be

$("#edit_2").focus();

EDIT : If you are wondering why the first line was not counted as a syntax error, it's because it is a correct statement saying "get the function focus " (and do nothing with it).

Your statement

$("#edit_2").focus

is not calling the function 'focus', in order to call the function you have to use the syntax 'focus()'

try

j$("#some_id").focus()

It is working fine.

EDIT Your statement '$("#edit_2").focus' is not throwing an error because it just returns a reference to the function 'focus', but it does not call the function.

focus is a function and must be called like one, change your code to look like:

$("#edit_2").focus();

For reference, see focus documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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