jQuery .toggle event deprecated, What to use?

邮差的信 提交于 2019-11-27 07:56:13

问题


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


回答1:


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




回答2:


var i = 0;    
$('button').click(function() { 
    if (i == 0){        
        $('div').css({background: 'red'}) 
        i++; 
    } else { 
        $('div').css({background: 'yellow'}) 
        i = 0; 
    }
});


来源:https://stackoverflow.com/questions/17583215/jquery-toggle-event-deprecated-what-to-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!