javascript - How to use jQ to bind events, and the events must support unbinding and passing parameters?
大家讲道理
大家讲道理 2017-05-19 10:15:05
0
3
587

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.

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(3)
漂亮男人

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”变量只在函数体内有效,不会污染外部
});
曾经蜡笔没有小新

Why can’t I unbind myself when I write anonymously? ? ?


    $("object").bind("click",function(e){
    if('達到某一條件??') {
         $(this).unbind();
    }
});
小葫芦

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'); // 解绑
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template