jQuery自定义事件数据(订阅和触发器)

Ale*_* Dn 5 javascript jquery javascript-events

我想弄清楚,我如何为自定义事件设置参数.我在订阅事件时如何设置参数,然后在触发事件时添加一些额外的数据.

我有一个简单的JS用于测试,但在"handle"的e参数中,我只看到了subscribe的数据.

function handle(e) {
    //e.data has only "b"
    alert(e.data);
}

function myObj() {
    this.raise = function () {
            //Trigger
        $(this).trigger("custom", { a: "a" });
    }
}

var inst = new myObj();
//Subscribe
$(inst).bind("custom", { b: "b" }, handle);
inst.raise();
Run Code Online (Sandbox Code Playgroud)

谢谢.

Poi*_*nty 5

提供.trigger()的参数作为事件处理函数的第二个参数传递.

function handle(e, triggerParam) {
    //e.data has only "b"
    alert(e.data + ' also ' + triggerParam);
}
Run Code Online (Sandbox Code Playgroud)