根据JQuery文档,可以像这样声明命名空间,以便与on
和off
:一起使用
var validate = function () {
// code to validate form entries
};
// delegate events under the ".validator" namespace
$("form").on("click.validator", "button", validate);
$("form").on("keypress.validator", "input[type='text']", validate);
// remove event handlers in the ".validator" namespace
$("form").off(".validator");
但是,如何为多事件声明声明一个命名空间,以便稍后使用off()
解除绑定?
$('#frame-right').on('mousewheel DOMMouseScroll MozMousePixelScroll',function(event){
event.preventDefault();
return false;
});
$('#frame-right').off(???)
您必须为每个单独的事件名称添加名称空间:
$("#frame-right").on(
"mousewheel.ns DOMMouseScroll.ns MozMousePixelScroll.ns",
function(event) {
event.preventDefault();
return false;
}
);
然后你可以发布:
$("#frame-right").off(".ns");