Toggling click handlers in Javascript

后端 未结 6 744
庸人自扰
庸人自扰 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:12

    This would help add data-click-state attribute on your button

    $(document).ready(function() {
        $('#mybutton').on('click', function() {
            if ($(this).attr('data-click-state') == 1) {
                $(this).attr('data-click-state', 0)
                myFirstHandlerFunction();
            } else {
                $(this).attr('data-click-state', 1)
                mySecondHandlerFunction();
            }
        });
    });
    

提交回复
热议问题