我使用Bootstrap-Notify script向用户显示出色的视觉警报。但要使用脚本的全部优势,我想挂钩events of the script。遗憾的是,文档或其他开源平台(如GitHub)都没有示例。
如上面链接的文档中所述,我将onClose
属性与函数一起使用:
$(document).ready(function() {
//Notifications
var welcome = $.notify({
message: "Test"
}, {
onClose: test()
});
});
function test() {
console.log("onClose");
}
我已经制作了一个short fiddle来告诉您它没有按照思路工作(在我的情况下,test
函数应该在通知触发时调用在脚本开始)。其他方法如welcome.onClose(test());
也失败了。
那么,如何成功地挂钩Bootstrap-Notify事件?
答案 0 :(得分:2)
问题是您没有将参考作为通知test
函数传递给onClose
,而是传递了结果功能!
你想这样做:
$(document).ready(function() {
//Notifications
var welcome = $.notify({
message: "Test"
}, {
onClose: test
});
});
或者如果您只想修改test
函数(而不是$ .notify部分),则需要执行此操作:
function test() {
return function() {
console.log("onClose");
};
}
onClose
的参数应该是一个函数