.click function in jquery always have to get a nameless function?
Hallo all. I have a little problem.
开发者_如何转开发I have a function. Let’s call it :
function rowClick ()
{
}
Anyway, the question is:
How can I bind this function to a click event without it being called?
What I mean is, if I do this:
$("#holder").click (function(){ rowClick(); });
the rowClick
function gets called while registering it to the click event.
Anyway I understand why this happens. I just don’t understand how I can bind the function to the event in a way that it won’t be called.
Thanks.
Remove the function() and parenthesis from the function name, ex:
$("#holder").click(rowClick);
How about...
$("#holder").click (rowClick);
...although the anonymous function should not be invoked (and, therefore, neither should rowClick) until the click event occurs.
You just have to pass a function reference as parameter. That does not neccesarily mean an anonymous function
.
The call
$("#holder").click ( rowClick );
will work just fine. Don't forget to redesign your rowClick
function with an event parameter
:
function rowClick (event) {
alert(event.target.id);
}
This parameter is passed in automatically and can be accessed within your callback function.
精彩评论