Toggling click handlers in Javascript

后端 未结 6 880
庸人自扰
庸人自扰 2021-01-05 14:54

I have an HTML button to which I attach an event, using jQuery\'s bind(), like so:

$(\'#mybutton\').bind(\'click\', myFirstHandlerFunction);
         


        
6条回答
  •  灰色年华
    2021-01-05 15:33

    Update: Since this form of toggle() was removed in jQuery 1.9, the solution below does not work anymore. See this question for alternatives.

    It looks like toggle() would solve your problem:

    $("#mybutton").toggle(myFirstHandlerFunction, mySecondHandlerFunction);
    

    The code above will register myFirstHandlerFunction and mySecondHandlerFunction to be called on alternate clicks.

提交回复
热议问题