在 Javascript/jQuery 中将匿名函数传递给自定义事件触发器

发布于 2024-11-16 03:53:24 字数 492 浏览 11 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城孤痞 2024-11-23 03:53:24

您需要将多个额外参数作为数组传递给 trigger()(可以在没有数组的情况下传递一个参数。)

$(some-dom).click(function() {      //  v-----pass extra args in an Array
    $(some-dom).trigger("custom_event", [some_data, function () {
        alert("this is my anonymous function passed as event data");
    }]);
 //  ^------Array
});

示例: http ://jsfiddle.net/NRSJ2/

You need to pass multiple extra arguments to trigger() as an Array. (One argument can be passed without the Array.)

$(some-dom).click(function() {      //  v-----pass extra args in an Array
    $(some-dom).trigger("custom_event", [some_data, function () {
        alert("this is my anonymous function passed as event data");
    }]);
 //  ^------Array
});

Example: http://jsfiddle.net/NRSJ2/

围归者 2024-11-23 03:53:24

您可以这样做:

$('#testelement').live("test_event", function(e, myCallback) {
    myCallback();
});

$('#clickme').click(function(e){
    e.preventDefault();

    var myFunc = function () {
        alert("this is my anonymous function passed as event data");
    };

    $('#testelement').trigger('test_event', [myFunc]);
});

这是概念证明

You can do it like this:

$('#testelement').live("test_event", function(e, myCallback) {
    myCallback();
});

$('#clickme').click(function(e){
    e.preventDefault();

    var myFunc = function () {
        alert("this is my anonymous function passed as event data");
    };

    $('#testelement').trigger('test_event', [myFunc]);
});

Here is the proof of concept.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文