代码之家  ›  专栏  ›  技术社区  ›  ilija veselica

传递$(this)作为参数?

  •  7
  • ilija veselica  · 技术社区  · 15 年前
    $(document).ready(function() {
        function GetDeals() {
        alert($(this).attr("id"));
    }
    
    $('.filterResult').live("click", function(event) {
        GetDeals();
    });
    

    (});

    函数中需要传递什么参数 GetDeals() 这样我就可以操纵 $(this) ?

    事先谢谢!

    2 回复  |  直到 13 年前
        1
  •  14
  •   gnarf    15 年前

    您可以使用函数作为事件句柄:

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

    (请注意,您不使用 () 调用函数,因此函数本身被传递给 live() 函数,而不是其结果。

    或者你可以使用 Function.prototype.apply()

    $('.filterResult').live("click", function(event) {
      GetDeals.apply(this);
    });
    
        2
  •  4
  •   rjha94    13 年前

    上面的解决方案是可行的,而且绝对没有问题。然而,我认为更好的模式是:

    $('.filterResult').live("click", function(event) {
        GetDeals($(this));
    });
    
    
    function GetDeals(linkObj) {
        var id = $(linkObj).attr("id");
        console.log(id);
    }