Excuse me, use jQ to bind a mousemove event, and the event needs to pass in parameters, and must support unbinding. How to implement this?
If you use an anonymous function, you cannot unbind it. If it is not anonymous, it seems that you cannot pass parameters.
漂亮男人2017-05-19 10:17:05
Just give the anonymous function an internal name.
$(element).on('mousemove',{a:1},function handler(ev){
console.log(ev.data);//{a:1}
$(this).off('mousemove',handler);//“handler”变量只在函数体内有效,不会污染外部
});
曾经蜡笔没有小新2017-05-19 10:17:05
Why can’t I unbind myself when I write anonymously? ? ?
$("object").bind("click",function(e){
if('達到某一條件??') {
$(this).unbind();
}
});
高洛峰2017-05-19 10:17:05
Named function binding, parameter passing and unbinding:
function omg(event) {
console.info(event.data.foo);
}
$('#omg').on('mousemove.omg', {foo: "bar"}, omg); // 绑定及传参
$('#omg').off('.omg'); // 解绑
Anonymous function binding, parameter passing and unbinding:
$('#omg').on('mousemove.omg', {foo: "bar"}, function (event) {
console.info(event.data.foo);
}); // 绑定及传参
$('#omg').off('.omg'); // 解绑