使用代理时,jQuery不显示事件名称空间 [英] jQuery not showing event namespace when using proxy
问题描述
在我的脚本中,我希望具有两种类型的单击功能,具体取决于所单击的内容,并且我希望在单个功能中执行此操作.我决定使用名称空间来创建它,但是控制台给了我未定义的内容;
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;
这是代码:
$target1.on("click.Bst", $.proxy(self.selectNav, self));
$target2.on("click.Bst.Type2", $.proxy(self.selectNav, self));
我的函数selectNav()在单击时执行,但是当我尝试获取event.namespace(事件作为s函数参数传递)时,我无法定义.
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"
}
推荐答案
event.namespace
仅在事件是通过.trigger
(例如$target1.trigger('click.Bst')
)而非本地DOM事件触发时定义的.与.proxy
无关.
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.
这篇关于使用代理时,jQuery不显示事件名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!