jQuery .toggle event deprecated, What to use?

2019-01-20 09:34发布

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

2条回答
我想做一个坏孩纸
2楼-- · 2019-01-20 10:10

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

查看更多
地球回转人心会变
3楼-- · 2019-01-20 10:13
var i = 0;    
$('button').click(function() { 
    if (i == 0){        
        $('div').css({background: 'red'}) 
        i++; 
    } else { 
        $('div').css({background: 'yellow'}) 
        i = 0; 
    }
});
查看更多
登录 后发表回答