.on().off()之后的jQuery事件

cha*_*nge 3 jquery

我有一个.on('click','td', function(){})活动.我打开它.off('click','td').我现在想.on()再次举办同样的活动.我尝试了一些组合,.on()它没有用.可以通过将其分配给变量来完成,但不知道如何做到这一点.

Rut*_*rde 8

将事件处理函数放在变量中,并像这样使用它:

var handler = function(e){
    //code here
}

//on
$('#myselector').on('click', handler);

//off
//Try this, this will only turn 'handler' off
$('#myselector').off('click', handler);

//If the above doesn't work, then try this
//This will turn off all your other click handlers for the same element,
//if for some reason turning off a particular handler doesn't work!
$('#myselector').off('click');

//on again
$('#myselector').on('click', handler);
Run Code Online (Sandbox Code Playgroud)