我有一个.on('click', 'td', function(){})
事件。
我将其关闭.off('click', 'td')
。现在我想再次绑定同样的事件.on()
。我尝试了一些.on()
的组合,但是没有成功。可能需要将其分配给一个变量,但不确定如何做。
我有一个.on('click', 'td', function(){})
事件。
我将其关闭.off('click', 'td')
。现在我想再次绑定同样的事件.on()
。我尝试了一些.on()
的组合,但是没有成功。可能需要将其分配给一个变量,但不确定如何做。
将事件处理函数放入变量中,然后像下面这样使用:
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);
我假设您希望点击TD时会发生以下情况:首先给您的td添加一个类来表明它是开启还是关闭状态。默认情况下,TD将具有td-off类,如下所示:
<td class="td-off">
.on('click','td',function(){
if($(this).hasClass('td-off')){
$(this).removeClass('td-off');
$(this).addClass('td-on');
}else{
$(this).removeClass('td-on');
$(this).addClass('td-off');
}
});
$('.td-on').click(function(){
//Your event on
});
$('.td-off').click(function(){
//Your event off
});
这可能不是最好的答案,但我希望你能理解