简体   繁体   English

jQuery事件上的自定义回调

[英]custom callback on jquery event

I am working with jquery and jstree 我正在使用jquery和jstree

I have an event that triggers every time my tree changes: 我有一个事件,每次我的树变时都会触发:

$tree.jstree()
    .on("changed.jstree",  function(event, target) {
        //manipulate data
    });

It works perfect. 完美的作品。 I can access "this" (the tree), and also event and target. 我可以访问“ this”(树)以及事件和目标。 But, I am trying to define a custom callback. 但是,我正在尝试定义一个自定义回调。 I tried something like this: 我尝试过这样的事情:

window.customCallback = (function(event, target) {
    //manipulate data
    //$(this).foo() manipulates the tree
    //event.type to access the event type
    //target.node to access the node
}(this));

So I can use: 所以我可以使用:

$tree.jstree()
    .on("changed.jstree",  customCallback(event, target));

But it doesn't work. 但这是行不通的。 Could somebody help me out? 有人可以帮我吗?

$tree.jstree().on("changed.jstree", customCallback(event, target)); $ tree.jstree()。​​on(“ changed.jstree”,customCallback(event,target));

What you're doing is setting the result of customCallback as callback handler. 您正在做的是将customCallback结果设置为回调处理程序。

What you want to do is set the function itself as callback handler: 要做的是将函数本身设置为回调处理程序:

var customCallback = function(event, target) {
// ...
};

$tree.jstree().on("changed.jstree", customCallback);

Notice the "missing" brackets - because brackets would invoke the function and we don't want that. 注意“缺失”括号-因为括号会调用该函数,我们不希望这样。

The parameters will be passed to the handler automatically. 参数将自动传递给处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM