开发者

jQuery this var outside of anon function

开发者 https://www.devze.com 2023-03-18 20:37 出处:网络
I\'m trying to achieve something like this. Any ideas? function test(that){ $(that).blur(); } $(\"#form\").focus(tes开发者_JS百科t(this));

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.

0

精彩评论

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