jQuery .toggle event deprecated, What to use?

前端 未结 2 646
逝去的感伤
逝去的感伤 2020-12-11 17:46

Since the jQuery .toggle event method is deprecated. What are we suppose to use to simulate this event (alternate clicks)?

2条回答
  •  隐瞒了意图╮
    2020-12-11 18:30

    Add this outside of document.ready

    $.fn.clicktoggle = function(a, b) {
        return this.each(function() {
            var clicked = false;
            $(this).click(function() {
                if (clicked) {
                    clicked = false;
                    return b.apply(this, arguments);
                }
                clicked = true;
                return a.apply(this, arguments);
            });
        });
    };
    

    then use the following to replicate the .toggle functionality:

    $("#mydiv").clicktoggle(functionA,functionB);
    

    Found on the JQuery Forums

提交回复
热议问题