从jQuery事件访问函数中的参数*和*事件 [英] Accessing Parameters *and* Events in function from jQuery Event

查看:200
本文介绍了从jQuery事件访问函数中的参数*和*事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对不同问题的后续问题,我不是很久以前问的。通常,您可以从jQuery事件的函数调用中访问事件,如下所示:

  $ item.live ,functionToCall); 

并在函数中:

  function functionToCall(ev){
//在这里做一些事情,比如检查'ev.target'
}
/ pre>

但是如果我想向functionToCall()发送一个参数访问事件呢?所以这样的东西,也许? :

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

  function functionToCall(ev,$ clickedItem){
//两者都可以在这里访问
alert(ev.type);
alert($ clickedItem.attr('id'))
}

这是可以接受的,还是有不同的方式发送参数?



CLARIFICATION:我意识到一个匿名的回调函数将允许我访问这两个,但由于各种原因太长的时间进入这个职位,我需要使用一个函数调用而不是匿名函数所以我的问题严格地处理一个外部函数需要被调用的情况。非常感谢。



UPDATE:我的原始问题提出了需要将$(this)作为参数传递给外部函数的场景。事实证明,$(this)将可以在函数中访问,甚至不需要传递它,因为jQuery重新分配值到这基于事件的方式。因此,执行此代码应该适用于我的原始问题:

  $ item.live(click,functionToCall); 

  function functionToCall(ev){
alert(ev.type);
alert($(this).attr('id')); //显示被点击的项目的ID
}

有一个不同的场景,涉及需要传递一个不同类型的变量作为参数,如简单的字符串或int。在这种情况下,如其他人所注意到的,它变得更加复杂。但是,在这里似乎有足够的答案来满足这第二种情况(即currying)。感谢。

解决方案

您可以 curry 或部分应用您的功能:



类似这样:

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

然后你可以使用它: p>

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

注意:如果您无法修改原始 functionToCall 是外部的,你可以换行:

  function wrapFunctionToCall($ clickedItem){
return function {
//调用原函数:
functionToCall(ev,$ clickedItem);
}
}
// ...
$ item.live (click,wrapFunctionToCall($(this));


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:

$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? 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. 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. 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. 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"). 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:

function wrapFunctionToCall($clickedItem) {
  return function (ev) {
    // call original function:
    functionToCall(ev, $clickedItem);
  }
}
// ...
$item.live("click", wrapFunctionToCall($(this));

这篇关于从jQuery事件访问函数中的参数*和*事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆