2014-06-11 52 views
0

解除綁定()我有這樣的代碼:綁定()和click()函數

$('#people_nav>.prev, #people_nav>.next').click(function(){ 

    // Here I need to disable event of click for needed elements, see code below 
    $('#people_nav>.prev, #people_nav>.next').unbind('click'); 

    ... 
    code 
    ... 

    // Here I need resume event for click() function. How? 

}); 

我怎樣才能恢復事件click()功能?它應該在click(function(){...}); ...

+0

你是什麼意思「恢復」?您是否想要暫時禁用任何點擊事件,或者您是否希望該事件被暫停並重新開始? – thomaux

回答

0

您可以使用命名函數。像

var element = $('#people_nav>.prev, #people_nav>.next'); //cache for better performance 
var yourFunction = function() { 
    // Here I need to disable event of click for needed elements, see code below 
    element.off('click'); //unbind event 
     ... 
    code 
     ... 
    // Here I need resume event for click() function. How? 
    element.click(yourFunction); 
}; 
element.click(yourFunction); 

但是什麼東西,我會建議使用on()off()

0

你只需要使用off

$('#people_nav>.prev, #people_nav>.next').off('click'); 

如果jQuery的版本> = 1.7寧願onoff方法而不是bindunbind方法

1

您可以使用on()off()是什麼,以及一個名爲功能

var elements = $('#people_nav > .prev, #people_nav > .next'); 

elements.on('click', doStuff); // bind handler 

function doStuff() { 

    elements.off('click'); // unbind handler 

    /*... 
    code 
    ...*/ 

    elements.on('click', doStuff); // rebind handler 
});