how to unbind and bind again

前端 未结 5 1126
野性不改
野性不改 2021-01-23 00:59
$(\"#archive\").click(function(event){
        /*do something*/
});

$(\'#archive2\').unbind(\'click\',event);

i have this click function that I unbind

5条回答
  •  猫巷女王i
    2021-01-23 01:20

    You have to keep a reference to the function (instead of passing an anonymous function):

    function handler() {
        // do something
    }
    
    $("#archive").click(handler); // bind the first time
    $("#archive").unbind('click', handler); // unbind
    $("#archive").click(handler); // bind again
    

    Not sure what is event in your case, but if it is the event object passed to the event handler then it does not make sense to pass it to unbind and bind.

提交回复
热议问题