Passing $(this) to functions
In JQuery开发者_开发知识库, how we can pass entire $(this) to a functions? Possible?
Um, it depends on what you are passing, but have you just tried the following:
function do_something_cool(jquery_link_object) {
/* I do something awesome! */
}
$('a.my_link_class').click(function() {
do_something_cool($(this));
});
This will pass the clicked link to the do_something_cool
method.
Your question isn't very specific, but maybe you want something like this:
$("a.myLinkClass").click(
function(){
$(this).addClass('myClickedLinkClass'); // 'this' refers to the anchor element that was clicked
}
);
The $(this)
element is exposed in most jQuery event operations. You can add a functional parameter to your jQuery handlers.
$("a.someClass").live("click", function(e) {
// $(this) is a reference to the same <a> element.
var link = $(this);
doSomethingToLink(link);
});
精彩评论