jQuery this var outside of anon function
I'm trying to achieve something like this. Any ideas?
function test(that){
$(that).blur();
}
$("#form").focus(tes开发者_JS百科t(this));
You have to pass focus
a function. You are putting a function call inside it, and that is returning undefined
.
function test(){
this.blur();
}
$("#form").focus(test);
Don't blur elements onfocus though — it creates major accessibility issues and effectively prevents anyone from navigating past the element without a pointing device.
Can you do this ?
.focus(function() {
$(this).blur();
)};
Assuming #form
is the id of a form, you'll need to put the focus listener on the input:
function test(obj)
{
$(obj).blur();
}
$('#form input').focus(function()
{
test($(this));
}
You should do:
$("#form").focus(function(){
test(this);
});
This is because event handlers must be functions, it means that the argument of the .focus()
method has to be a function, you cannot write code "directly" in the parameter as you did.
Hope this helps.
精彩评论