gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 04:10:36 24 4
gpt4 key购买 nike

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

我有一个简单的 JS 用于测试,但是在“句柄”的 e 参数中我只看到订阅的数据。

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();

最佳答案

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

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

关于javascript - jQuery 自定义事件数据(订阅和触发),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11295453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com