How to bind, unbind and rebind (click) events in JQuery

前端 未结 2 1357
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 05:57

After asking the same question 2 weeks ago here, I finally found \"the\" solution. This is why I am answering my own question. ;)

HOW TO BIND, UNBIND AND REB

2条回答
  •  隐瞒了意图╮
    2020-12-03 06:24

    And THIS is the perfect solution. (At least for me.)

    $(document).on('click', 'a#button', function(){
        $(this).after(' hello');
        $('span').fadeOut(1000);
    });
    
    $('a#toggle').toggle(
        function(){
            $(this).text('rebind');
            $('a#button').on('click.disabled', false);
        },
        function(){
            $(this).text('unbind');
            $('a#button').off('click.disabled');
        }
    );
    

    ".on()" does it. NOTE: It is important to bind the event like you can see on line one! It does not work with .click() or …!

    See the docs!

    Here is a fiddle to try it and experiment with it!

    Enjoy!

提交回复
热议问题