最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pass $(this) as argument? - Stack Overflow

programmeradmin1浏览0评论
$(document).ready(function() {
    function GetDeals() {
    alert($(this).attr("id"));
}

$('.filterResult').live("click", function(event) {
    GetDeals();
});

});

What do I need to pass as argument in function GetDeals() so that I can manipulate with $(this)?

Thanks in advance!

$(document).ready(function() {
    function GetDeals() {
    alert($(this).attr("id"));
}

$('.filterResult').live("click", function(event) {
    GetDeals();
});

});

What do I need to pass as argument in function GetDeals() so that I can manipulate with $(this)?

Thanks in advance!

Share Improve this question edited Aug 9, 2011 at 17:49 abatishchev 100k88 gold badges301 silver badges442 bronze badges asked Jun 4, 2010 at 8:07 ilija veselicailija veselica 9,57439 gold badges95 silver badges151 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 14

You could just use the function as your event handle:

$('.filterResult').live("click", GetDeals);

(please note, you don't use the () to call the function, so the function itself is being passed to the live() function, not its result.

Or you can use Function.prototype.apply()

$('.filterResult').live("click", function(event) {
  GetDeals.apply(this);
});

Above solution works and absolutely no issues with that. However I believe a better pattern here is :

$('.filterResult').live("click", function(event) {
    GetDeals($(this));
});


function GetDeals(linkObj) {
    var id = $(linkObj).attr("id");
    console.log(id);
}
发布评论

评论列表(0)

  1. 暂无评论