jQuery not showing event namespace when using prox

2019-08-30 10:24发布

问题:

In my script I want to have two types of click functionality, depend on what is clicked and I want to do it in a single function. I decided to make it with namespaces, but the console gives me undefined;

Here is the code:

$target1.on("click.Bst", $.proxy(self.selectNav, self));            

$target2.on("click.Bst.Type2", $.proxy(self.selectNav, self));

My function selectNav() executes on click, but when I try to get the event.namespace (event is passed as s function parameter) I get undefined.

selectNav: function(event){
 //other code runs  
 console.log(event.namespace);  //console log "undefined"
}

回答1:

event.namespace is only defined when the event was triggered via .trigger (e.g. $target1.trigger('click.Bst')), not through native DOM events. It has nothing to do with .proxy.

I have to admit that the documentation could be clearer about this though.