识别来电者

时间:2011-06-15 15:48:13

标签: javascript jquery

我在一些html中有一个链接,它调用了我正在编写的javascript函数。调用必须是内联的,因此代码如下:

 <a href='#' onclick='quipManager.getQuip(1, $(this))'>Test</a>

在getQuip函数中,我使用this引用来修改通过.parents()包装链接的div e.g。

  getQuip: function (id, currentQuip) {
      alert(currentQuip.parents('.qtip').html());
  }

我需要保持内联呼叫,但我想知道的是我是否可以像这样使用它:

 <a href='#' onclick='quipManager.getQuip(1)'>Test</a>

并且仍然知道getQuip知道哪个链接调用了它,所以以下内容仍然有效。

 getQuip: function (id) {
      var currentQuip = id.caller ???
      alert(currentQuip.parents('.qtip').html());
  }

2 个答案:

答案 0 :(得分:1)

您正在寻找类似的东西:

$('#element').click(function() {
    example.apply(this);
});

var example = function() {
    $(this).text("it worked");
}

.apply将允许您在示例函数中更改this的默认赋值,因此您可以将其更改为任何元素,并将其处理为相同。

http://jsfiddle.net/mwrX2/

答案 1 :(得分:0)

我猜答案是你不能:(