开发者

How to send "this" to named function in event binding statement?

开发者 https://www.devze.com 2023-03-23 23:21 出处:网络
I am bit stuck, don\'t know if its开发者_如何学运维 possible to turn: $("#id").click(function(){

I am bit stuck, don't know if its开发者_如何学运维 possible to turn:

$("#id").click(function(){  
    var that = jQuery(this);  
    wildFunction(that);
});  

into

$("#id").click(wildFunction(this));


"this" is a scope variable so it would be present in the event handler no matter how you bind it. so this would work:

$("#id").click(wildFunction);

function wildFunction()
{
    var that = $(this); // this is valid here, and we can make jQuery object with it
}


No, you can't do that — or rather, you could, but it wouldn't be useful. You need to have that function syntax there. In your second code sample, this would be whatever it is in the code calling click, where I'm assuming from your first code sample you want it to be the this that jQuery sets up for the event handler.


Nope, but you can use

$("#id").click( wildFunction );

and inside the wildFunction method this will refer to the #id DOM element.

0

精彩评论

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