【发布时间】:2011-06-10 18:28:17
【问题描述】:
我对命名空间自定义事件应该如何在 jQuery 中工作感到困惑。我从the doc 得到的印象是,触发命名空间自定义事件只会触发专门绑定到该事件的处理程序。相反,似乎命名空间几乎被忽略了。下面的示例和此处的实时代码:http://jsfiddle.net/kZCBw/1/
$(document)
.bind("reset.one", function(){ console.log("reset.one event detected");})
.bind("reset.two", function(){ console.log("reset.two event detected");})
.bind("cheese", function(){ console.log("cheese event detected");});
$("#btn1").click(function(){
console.log("firing reset.one event");
$(this).trigger("reset.one");
});
$("#btn2").click(function(){
console.log("firing reset.two event");
$(this).trigger("reset.two");
});
$("#btn3").click(function(){
console.log("firing reset event");
$(this).trigger("reset");
});
//btn1 click should only trigger handlers bound to "reset.one"
//and yet it triggers anything starting w/ "reset"
我错过了什么?
提前致谢! -马特
【问题讨论】:
-
旁注:与 jQuery 2.1.4 和 jQuery 3.0 配合得很好
标签: jquery