在 Javascript/jQuery 中将匿名函数传递给自定义事件触发器
我试图在 DOM 元素上触发自定义事件,并传递匿名函数以在事件触发时执行(使用 jQuery)。所以像这样:
$(some-dom).live("custom_event", function(evtObj, data, callback) {
//do some stuff
callback();
});
$(some-button).click(function() {
$(some-dom).trigger("custom_event", some_data, function () {
alert("this is my anonymous function passed as event data");
}
});
所以点击“some-button”应该触发“some-dom”上的“custom_event”并导致我传递给触发器的匿名函数被执行。正确的?但浏览器说自定义事件中未定义回调。我做错了什么吗?是否不允许将匿名函数作为触发器参数传递?谢谢
I'm trying to trigger custom events on DOM elements and pass anonymous functions to be executed when the event is triggered (using jQuery). So something like this:
$(some-dom).live("custom_event", function(evtObj, data, callback) {
//do some stuff
callback();
});
$(some-button).click(function() {
$(some-dom).trigger("custom_event", some_data, function () {
alert("this is my anonymous function passed as event data");
}
});
So clicking on "some-button" should trigger "custom_event" on "some-dom" and cause the anonymous function that I passed on the trigger to be executed. Right? But the browser says that callback is undefined in the custom event. Am I doing something wrong? Is passing anonymous functions as trigger arguments not allowed? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将多个额外参数作为数组传递给
trigger()
。 (可以在没有数组的情况下传递一个参数。)示例: http ://jsfiddle.net/NRSJ2/
You need to pass multiple extra arguments to
trigger()
as an Array. (One argument can be passed without the Array.)Example: http://jsfiddle.net/NRSJ2/
您可以这样做:
这是概念证明。
You can do it like this:
Here is the proof of concept.