简体   繁体   English

从jQuery Event访问函数中的参数*和*事件

[英]Accessing Parameters *and* Events in function from jQuery Event

This is a follow-up question to a different question I asked not too long ago. 这是我不久前问过的另一个问题的后续问题。 Typically, you can access an event in a function call from a jQuery event like this: 通常,您可以从jQuery事件访问函数调用中的事件,如下所示:

$item.live("click", functionToCall);

and in the function: 并在功能:

function functionToCall(ev) {
  // do something with ev here, like check 'ev.target'
}

But what if I wanted to send a parameter to functionToCall() and access the event? 但是如果我想将一个参数发送到functionToCall() 访问该事件呢? So something like this, maybe? 也许这样的事情呢? :

$item.live("click", functionToCall($(this));  // send over parameter this time

and

function functionToCall(ev, $clickedItem) {
  // both accessible here?
  alert(ev.type);
  alert($clickedItem.attr('id'));
}

Would that be acceptable, or is there a different way to send a parameter? 这是可以接受的,还是有不同的方式发送参数? Because this way doesn't seem right to me. 因为这种方式对我来说似乎不对。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

CLARIFICATION: I realize that an anonymous callback function would allow me to access both, but for various reasons too lengthy to get into in this post, I need to use a function call rather than the anonymous function. 澄清:我意识到匿名回调函数允许我访问这两个函数,但由于各种原因,在这篇文章中过于冗长,我需要使用函数调用而不是匿名函数。 So my question deals strictly with the scenario when an external function needs to be called. 所以我的问题严格处理需要调用外部函数的场景。 Thanks. 谢谢。

UPDATE: My original question presented the scenario of needing to pass $(this) as a parameter to the external function. 更新:我的原始问题提出了需要将$(this)作为参数传递给外部函数的场景。 As it turns out, $(this) will be accessible in the function without even needing to pass it, because of the way jQuery reassigns values to "this" based on events. 事实证明,$(this)将在函数中可访问,甚至不需要传递它,因为jQuery基于事件将值重新分配给“this”。 So performing this code should work for my original question: 所以执行此代码应该适用于我原来的问题:

$item.live("click", functionToCall);

and

function functionToCall(ev) {
  alert(ev.type);
  alert($(this).attr('id'));  // display id of item that was clicked
}

However, as others have answered, there is a different scenario that involves needing to pass a different kind of variable over as a parameter, such as a simply string or int. 但是,正如其他人已经回答的那样,有一种不同的场景需要将不同类型的变量作为参数传递,例如简单的字符串或int。 In this case, as others have notes, it becomes more complicated. 在这种情况下,正如其他人所说,它变得更加复杂。 But there do seem to be sufficient answers here to satisfy this second scenario (namely, "currying"). 但这里似乎有足够的答案来满足第二种情况(即“currying”)。 Thanks. 谢谢。

You can curry or partially apply your function: 您可以咖喱或部分应用您的功能:

Something like this: 像这样的东西:

function functionToCall($clickedItem) {
  return function (ev) {
    // both accessible here
    alert(ev.type);
    alert($clickedItem.attr('id'));
  }
}

Then you can use it like you want: 然后你可以像你想要的那样使用它:

$item.live("click", functionToCall($(this)); 

Note: If you can't modify your original functionToCall because is "external", you can wrap it: 注意:如果您不能修改原来的functionToCall因为它是“外部”,您可以将其包装:

function wrapFunctionToCall($clickedItem) {
  return function (ev) {
    // call original function:
    functionToCall(ev, $clickedItem);
  }
}
// ...
$item.live("click", wrapFunctionToCall($(this));
.live('click', functionA(prm) {
   return function(ev) {
     // here you can use the prm and the event
  }
}

我知道问题的答案,但是对于它的价值,如果您不想编辑原始函数,这里的单行程也可以正常工作:

$item.live("click", function() { functionToCall( $(this) ); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM