开发者

Passing $(this) to functions

开发者 https://www.devze.com 2022-12-22 14:42 出处:网络
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:

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);
});
0

精彩评论

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