使用参数连接回调动作侦听器 [英] Connecting a callback action listener WITH parameters

查看:61
本文介绍了使用参数连接回调动作侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用以下内容时,一切运作良好。


butt1.on(''click'',function(){sameStuff(''1'') });

butt2.on(''click'',function(){sameStuff(''''')});


现在,我希望将它压缩成一个电话,而不是两个匿名

函数。问题是我需要发送一个函数作为

的第二个参数。如果我使用以下内容我只返回,而不是输入
作为函数。


butt1.on(''click'',function(' '1''));


如何在

中输入指定参数的第二个参数BUT?它是否可行?


-

问候

Konrad Viltersten

When i''m using the following, everything works well.

butt1.on (''click'', function () {sameStuff (''1'')});
butt2.on (''click'', function () {sameStuff (''2'')});

Now, i wish to compact it into one call, instead of two anonymous
functions. The problem is that i''m required to send in a function as the
second parameter. If i use the following i get only a return back, not
typed as function.

butt1.on (''click'', function (''1''));

How can i enter as the second parameter BUT with a specified parameter in
it? Is it doable at all?

--
Regards
Konrad Viltersten

推荐答案

5月22日上午7:47,K Viltersten < t ... @ viltersten.comwrote:
On May 22, 7:47 am, "K Viltersten" <t...@viltersten.comwrote:

当我使用以下内容时,一切正常。


butt1.on(''click'',function(){sameStuff(''1'')});

butt2.on(''click'',function(){sameStuff (''''')});


现在,我希望将它压缩成一个调用,而不是两个匿名

函数。问题是我需要发送一个函数作为

的第二个参数。如果我使用以下内容我只返回,而不是输入
作为函数。


butt1.on(''click'',function(' '1''));


如何在

中输入指定参数的第二个参数BUT?它可行吗?
When i''m using the following, everything works well.

butt1.on (''click'', function () {sameStuff (''1'')});
butt2.on (''click'', function () {sameStuff (''2'')});

Now, i wish to compact it into one call, instead of two anonymous
functions. The problem is that i''m required to send in a function as the
second parameter. If i use the following i get only a return back, not
typed as function.

butt1.on (''click'', function (''1''));

How can i enter as the second parameter BUT with a specified parameter in
it? Is it doable at all?



我很难理解你想要做什么。


你是否想要定义一个函数可以作为两个按钮点击的

处理程序,并做一些有用的事情?如果那是'b
的情况,只需在两次调用on之前定义函数,

将它分配给一个变量,并在该变量中引用该变量两个

" on"电话。


在公共事件处理程序中,你可能必须知道点击了哪个按钮

,所以你必须得到事件对象(作为单个参数传递给
或全局事件对象)并获取

目标属性。


这有帮助吗?

I''m having trouble understanding what you''re trying to do.

Are you perhaps trying to define a single function that can act as the
handler for both button clicks, and do something useful? If that''s
the case, just define the function before the two calls to "on",
assigning it to a variable, and reference that variable in the two
"on" calls.

In the common event handler, you''ll probably have to know which button
was clicked, so you''ll have to get the event object (either passed in
as the single parameter, or the global event object) and get the
target property.

Does that help?


KVilterstenescribió:
K Viltersten escribió:

当我使用以下内容时,一切效果很好。


butt1.on(''click'',function(){sameStuff(''''')});

butt2。 on(''click'',function(){sameStuff(''''')});


现在,我希望将它压缩成一个调用,而不是两个匿名

功能。问题是我需要发送一个函数作为

的第二个参数。如果我使用以下内容我只返回,而不是输入
作为函数。


butt1.on(''click'',function(' '1''));


如何输入第二个参数BUT,其中包含指定参数

?它可行吗?
When i''m using the following, everything works well.

butt1.on (''click'', function () {sameStuff (''1'')});
butt2.on (''click'', function () {sameStuff (''2'')});

Now, i wish to compact it into one call, instead of two anonymous
functions. The problem is that i''m required to send in a function as the
second parameter. If i use the following i get only a return back, not
typed as function.

butt1.on (''click'', function (''1''));

How can i enter as the second parameter BUT with a specified parameter
in it? Is it doable at all?



根据Core JavaScript 1.5参考,您不能:


要将参数传递给事件处理程序,处理程序必须是

包装成另一个函数


document.form1.button1.onclick = function(){

setBGColor(''some值'');

};

http://developer.mozilla.org/en/docs...event_handlers

-

- http://alvaro.es - álvaroG。Vicario - 西班牙布尔戈斯

- Mi sitiosobreprogramaciónweb: http://bits.demogracia.com

- Mi web de humor al ba?oMaría: http:/ /www.demogracia.com

-

According to Core JavaScript 1.5 Reference, you cannot:

To pass parameters to an event handler, the handler must be
wrapped into another function"

document.form1.button1.onclick = function() {
setBGColor(''some value'');
};

http://developer.mozilla.org/en/docs...event_handlers
--
-- http://alvaro.es - álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al ba?o María: http://www.demogracia.com
--


>当我使用以下内容时,一切运作良好。
>When i''m using the following, everything works well.

> butt1.on(''click'',function(){sameStuff(''''')});
butt2.on(''click'',function(){sameStuff('''''' )});
现在,我希望将它压缩成一个调用,而不是两个匿名
函数。问题是我需要发送一个函数作为第二个参数。如果我使用以下内容我只返回,
不输入功能。
butt1.on(''click'',function(''''));
如何我可以输入第二个参数BUT并带有指定的参数吗?它可行吗?
> butt1.on (''click'', function () {sameStuff (''1'')});
butt2.on (''click'', function () {sameStuff (''2'')});
Now, i wish to compact it into one call, instead of two anonymous
functions. The problem is that i''m required to send in a function as
the second parameter. If i use the following i get only a return back,
not typed as function.
butt1.on (''click'', function (''1''));
How can i enter as the second parameter BUT with a specified parameter
in it? Is it doable at all?



根据Core JavaScript 1.5参考,您不能:


要将参数传递给事件处理程序,处理程序必须是

包装成另一个函数


document.form1.button1.onclick = function(){

setBGColor(''some值'');

};

http://developer.mozilla.org/en/docs...event_handlers



太糟糕了!谢谢。


-

问候

Konrad Viltersten

Too bad! Thanks.

--
Regards
Konrad Viltersten


这篇关于使用参数连接回调动作侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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